2012-04-18 58 views
0

我有PHP的web服務,我想叫它拋出AsyncTask ..在這個階段我沒有問題..我的問題是,我想讓一個類延伸AsyncTask並通過它的webservicesuri叫它多次在許多活動但我不知道如何做到這一點,並調用的AsyncTask ..任何幫助.. 謝謝如何調用asynctask很多很多服務在android

這是我曾嘗試但它不起作用

public class GetNetworkData extends AsyncTask<String, Void, String>{ 

    String uri; 
    private OnPostExecuteListener mPostExecuteListener = null; 
    public static interface OnPostExecuteListener 
    { 

       void onPostExecute(String result); 

      } 


    GetNetworkData(
      String Url, 
      OnPostExecuteListener postExecuteListener) throws Exception { 

      uri = Url; 
      mPostExecuteListener = postExecuteListener; 
      if (mPostExecuteListener == null) 
       throw new Exception("Param cannot be null."); 
     } 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     JSONObject param=null; 
     String result = null; 
     HttpClient httpClient = new DefaultHttpClient(); 
      HttpHost httpHost = new HttpHost("192.168.3.111",80); 
      HttpPost httpPost = new HttpPost(uri); 
      httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); 


      try 
      { 
       //HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8"); 
       //httpPost.setEntity(bodyEntity); 
       HttpResponse response = httpClient.execute(httpPost); 
       HttpEntity entity = response.getEntity(); 

       if (entity != null) { 
        InputStream instream = entity.getContent(); 
        BufferedReader reader = new BufferedReader(
         new InputStreamReader(instream)); 
        StringBuilder sb = new StringBuilder(); 


        String line = null; 
        while ((line = reader.readLine()) != null) 
         sb.append(line + "\n"); 


        result = sb.toString(); 
        instream.close();  
       } 

       } 
       catch (Exception e) { 
        // TODO: handle exception 
       } 

     return result; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     if (mPostExecuteListener != null) 
     { 
      try { 
        JSONObject json = new JSONObject(result); 
        mPostExecuteListener.onPostExecute(result); 
       } catch (JSONException e){ 
        e.printStackTrace(); 
       } 
      }   
    } 
} 

回答

0

無論何時何地您都可以創建GetNetworkData類的實例。如果UrlpostExecuteListener參數在所有調用中都相同,則可以考慮一個包裝類,它將這些值保存在實例變量中,並按需創建對象。

使用GetNetworkData的代碼是:

String url = "http://...."; 
GetNetworkData.OnPostExecuteListener listener = new GetNetworkData.OnPostExecuteListener() { 
    @Override 
    public void onPostExecute(String result) { 
     Log.d("AsyncTask", "onPostExecute: " + result); 
     //Do what you want to do after the task is complete 
    } 
}; 

GetNetworkData task = new GetNetworkData(url, listener); 
task.execute(); 
+0

url不一樣每個url都有自己的方法名稱 – Basant 2012-04-18 13:03:54

+0

在這種情況下,你可以在你需要的時候創建你的'GetNetworkData'類的實例。 – Rajesh 2012-04-18 13:05:05

+0

回答更新包括使用 – Rajesh 2012-04-18 13:20:28

0

進行呼不喜歡它,

String url = "so and so url"; 
new GetNetworkData().execute(url); 

,並在您的doInBackground(),

@Override 
protected String doInBackground(String... params) { 
    // TODO Auto-generated method stub 
    urlToUse = params[0];  
} 

每次執行AsyncTask ..它會去doInBackgroud()並執行該代碼..完成後n代碼執行..它會自動去onPostExecute()這是AsyncTask的LifeCycle。我想..你不需要爲此做任何事情。每次調用AsyncTask。

+0

和onPostExecute如何實現它爲每個響應 – Basant 2012-04-18 13:13:00

+0

請參閱更新 – MKJParekh 2012-04-18 13:18:14

+0

謝謝我會試試這個,我會再次回饋:) – Basant 2012-04-18 13:45:26