Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Android asynchronous post blocks UI thread

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...