2012-03-27 18 views
0

我是新來的異步任務。我需要在我的應用程序中使用httppost。請幫助我使用異步任務來處理以下代碼。從的AsyncTask請給我結構代碼如何使用異步任務來操縱httppost android

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

回答

0

看到這個http://developer.android.com/reference/android/os/AsyncTask.html。在doInBackground()方法中編寫用於操作HTTP Post.In的pInxecute()在運行線程之前處理UI,在線程完成後在onPostExecute()中處理UI。不要在doinBackground()中編寫UI相關代碼。

呼叫AsynTask線程,如下

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

SerivceCalling類:

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

} 
+0

謝謝你文卡塔克裏希納 – vinuonline 2012-03-27 07:09:12

0

使用此代碼

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

    } 
} 

這裏我使用的輸入參數字符串,返回值是ArrayList中。 根據您的要求而改變