Android异步后阻止UI线程

时间:2013-05-11 13:22:38

标签: android android-asynctask

我正在开发一款Android应用。我想使用asynctask发布到服务器。但是,我仍然有一个错误,表明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;
    }




}

}

2 个答案:

答案 0 :(得分:0)

我看到有几个问题。首先,您没有在execute()中传递任何内容,但在您的班级声明中,您告诉doInBackground()期望String。其次,您告诉onPostExecute()期待Document,但您从null返回doInBackground()而未在onPostExecute()中获取任何参数。除非我错过了什么,否则我看不出这是如何编译的

答案 1 :(得分: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 

  }});
}