Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm developing an Android app. I want to post to a server using asynctask. However, I still have an error which indicates that the UI thread is blocked.

I want to parse the XML response and display it in a list view, but I cannot proceed because the UI thread is still blocked.

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;
    }




}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are a couple problems that I see. First, you aren't passing anything in execute() but in your class declaration you are telling doInBackground() to expect a String. Secondly, you are telling onPostExecute() to expect a Document but you are returning null from doInBackground() and not taking any parameters in onPostExecute(). Unless I missed something, I don't see how this even compiles


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...