0

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();
                }
vinuonline
  • 143
  • 2
  • 3
  • 14

2 Answers2

0

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

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

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

}
Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19