How to use async task for manipulating the httppost android

会有一股神秘感。 提交于 2019-12-11 15:39:35

问题


I am new to async task . I need to use httppost in my application. Please help me to manipulate the following code using async task. Please give me structured code

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("url here");
                httpPost.addHeader("Content-Type", "application/xml");
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    StringBuffer responseString = new StringBuffer("");
                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseString.append(line);
                    }
                    System.out.println("respose QQQQQQQQQQQ");
                    System.out.println("11response "
                            + responseString.toString());

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

回答1:


see this http://developer.android.com/reference/android/os/AsyncTask.html. In doInBackground() method write your code for manipulating HTTP Post.In preexecute() handle UI before running thread and in onPostExecute() handle UI after thread completed.Don't write UI releated code in doinBackground().

call AsynTask thread as follows

ServerCalling  task = new ServerCalling();
            task.execute(new String[] { "Tickets" });

SerivceCalling class:

public  class ServerCalling extends  AsyncTask<String, Void, Void> {
              @Override
        protected void onPreExecute() {

        }

/**
         * On progress update.
         * 
         * @param unused
         *            the unused
         */
        protected void onProgressUpdate(Void unused) {
            L

og.d("HIIIIIIIIIIIIIIIIIIIIIIII","ONPROGRESS UPDATE IS CALLED ........................");
        }

             @Override
        protected void onPostExecute(Void unused) {
}

    @Override
        protected Void doInBackground(String... params) {
      HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("url here");
                httpPost.addHeader("Content-Type", "application/xml");
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    StringBuffer responseString = new StringBuffer("");
                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseString.append(line);
                    }
                    System.out.println("respose QQQQQQQQQQQ");
                    System.out.println("11response "
                            + responseString.toString());

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

}



回答2:


use this code

private class VerticalChannelTask extends AsyncTask<String, Void, ArrayList<MyChannelItem>> {       

    @Override
    protected void onPreExecute() { 
        super.onPreExecute();   
        show();
    }

    @Override
    protected ArrayList<MyChannelItem> doInBackground(String...params) { 
    /// place your code here i.e Http code..

        return m_orders;
    }       
    protected void onPostExecute(ArrayList<MyChannelItem> result) { 

        hide();         

    }
}

here i am using input parameter is String and return values from Asynctask was ArrayList. change depending on your requirement



来源:https://stackoverflow.com/questions/9884384/how-to-use-async-task-for-manipulating-the-httppost-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!