0

我正在开发一个 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;
    }




}

}
4

2 回答 2

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 

  }});
}
于 2014-05-30T08:54:32.760 回答
0

我看到了几个问题。首先,你没有传递任何东西,execute()但在你的类声明中你告诉doInBackground()期望一个String. 其次,您告诉onPostExecute()期望 aDocument但您正在null从返回doInBackground()并且没有在onPostExecute(). 除非我错过了什么,否则我什至看不到它是如何编译的

于 2013-05-12T05:26:28.587 回答