2013-05-11 71 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 

    }}); 
}