2013-05-11 68 views
0

我正在開發一個Android應用程序。我想使用asynctask發佈到服務器。但是,我仍然有一個錯誤,指出UI線程被阻塞。Android異步帖子塊UI線程

我想解析XML響應,並在列表視圖中顯示出來,但我不能繼續,因爲UI線程仍然受阻。

public class AsynchronousPost extends ListActivity implements OnClickListener { 

EditText SearchValue; 
Button SearchBtn; 
String URL = ""; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search_interface); 


    SearchBtn = (Button) findViewById(R.id.searchbtn); 


    SearchBtn.setOnClickListener(this); 
} 

public void onClick(View views) { 

new MyAsyncTask().execute(); 


} 

private class MyAsyncTask extends AsyncTask<String, Integer, Document> { 

    private final String URL = "url"; 

    private final String username = "username"; 
    private final String password = "password"; 
    private EditText SearchValue; 


    @Override 
    protected Document doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     getXmlFromUrl(URL); // getting XML 
     return null; 


    } 

      @Override 
    protected void onPostExecute() { 

      //want to parse xml response 
      //display on listview 
      } 


    public String getXmlFromUrl(String url) { 
     String xml = null; 

     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      SearchValue = (EditText) findViewById(R.id.search_item); 
      String Schvalue = SearchValue.getText().toString(); 

      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        5); 
      nameValuePairs 
        .add(new BasicNameValuePair("username", username)); 
      nameValuePairs 
        .add(new BasicNameValuePair("password", password)); 
      nameValuePairs.add(new BasicNameValuePair("searchItem", 
        Schvalue)); 

      // response stored in response var 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      xml = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     // return XML 
     return xml; 
    } 




} 

} 
+0

拋出什麼樣的異常? – 2013-05-11 14:08:42

+0

@Lukas錯誤是:應用程序商業目錄(進程com.knust.edu)意外停止。請再試一次** – kkStep 2013-05-11 17:11:57

+0

這就是Android顯示的內容。你的調試器告訴你什麼? – 2013-05-12 00:49:20

回答

0

我看到有幾個問題。首先,您沒有通過​​中的任何內容,但是在您的班級聲明中,您告知doInBackground()期望String。其次,你告訴onPostExecute()期待一個Document,但你從doInBackground()返回nullonPostExecute()不採取任何參數。除非我錯過了某些東西,否則我甚至不知道這是如何編譯的

0
protected Object doInBackground(String... params) { 
    //this method of AsyncTask is not running on the UI Thread -- here do just non UI   taks 
    return result; 
} 

@Override 
protected void onPostExecute(Object result) { 
    //I'm not sure but I think this method is running on the UI Thread 
    //If you have long operations here to do you will block UI Thread 
    //put the tasks in the doInBackground... 
    //to fill the elements in the UI elements use 
    // 
    runOnUiThread (new Runnable() { 
     @Override 
     public void run() { 
     //here fill your UI elements 

    }}); 
}