Skip to content Skip to sidebar Skip to footer

How To Use Async Task For Manipulating The Httppost Android

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

Solution 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

ServerCallingtask=newServerCalling();
            task.execute(newString[] { "Tickets" });

SerivceCalling class:

publicclassServerCallingextendsAsyncTask<String, Void, Void> {
              @OverrideprotectedvoidonPreExecute() {

        }

/**
         * On progress update.
         * 
         * @paramunused
         *            the unused
         */protectedvoidonProgressUpdate(Void unused) {
            L

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

             @OverrideprotectedvoidonPostExecute(Void unused) {
}

    @OverrideprotectedVoiddoInBackground(String... params) {
      HttpClient httpClient = newDefaultHttpClient();
                HttpPost httpPost = newHttpPost("url here");
                httpPost.addHeader("Content-Type", "application/xml");
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    BufferedReader reader = newBufferedReader(
                            newInputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    StringBuffer responseString = newStringBuffer("");
                    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();
                }

}

Solution 2:

use this code

privateclassVerticalChannelTaskextendsAsyncTask<String, Void, ArrayList<MyChannelItem>> {       

    @OverrideprotectedvoidonPreExecute() { 
        super.onPreExecute();   
        show();
    }

    @OverrideprotectedArrayList<MyChannelItem> doInBackground(String...params) { 
    /// place your code here i.e Http code..return m_orders;
    }       
    protectedvoidonPostExecute(ArrayList<MyChannelItem> result) { 

        hide();         

    }
}

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

Post a Comment for "How To Use Async Task For Manipulating The Httppost Android"