2015-12-12 15 views
4

我有一個外部庫可以通過互聯網與服務器通信。每次我需要從互聯網上獲取一些信息時,android都會迫使我使用asynctask。到目前爲止沒有問題。然而,我從越來越多的任務中檢索(以不同的方式)來自互聯網的數據,並且我不喜歡每次呼叫增加不同的類。 長話短說:我有不同的網絡電話,而不是爲每個電話創建一個asynctask-class,我會優先選擇一個班級來管理所有不同的電話。這是否有可能?更重要的是,這樣做的正確方法是什麼?如何管理需要由AsyncTask調用的不同任務

+0

Android不會強制您使用AsyncTasks - 有很多方法可以調用Web服務。也許類似Retrofit會有所幫助:https://github.com/square/retrofit – Intrications

回答

1

像你have.But我通過反射技術解決了這個問題,我也面臨similer問題。我已經制定了一個方法來防止增加不同的類來調用單擊。 我做了單一的asynctask類,並通過onPostExecute傳遞了活動的函數名和上下文並返回了響應。

下面是示例 -

AsyncTaskConnection.java

public class AsyncTaskConnection extends AsyncTask<String, String, Object>{ 
    JSONObject mainObject; 
    Context mContext; 
    String returnFunctionName; 

    public AsyncTaskConnection (Context context){ 
     mContext = context; 
    } 

    protected void onPreExecute() { 
     // preExecute 
    } 

    @Override 
    protected Object doInBackground(String... arguments) { 
     String apiFunctionName = arguments[0]; // get api FunctionName 
     String jsonString = arguments[1]; // get data 
     returnFunctionName = apiFunctionName+"Response"; // return function name 
     // some connection code... 


     //then call... 
      try { 
       ht.call(NAMESPACE, requestEnvelop); 
      } catch (IOException ex) { 
       Log.d(mContext.getClass().getName(), "Io exception bufferedIOStream closed" + ex); 
       ex.printStackTrace(); 
      } 
      return mainObject.toString(); 
     } catch (Exception e) { 
      Log.d("Exception", e.toString()); 
      return "no"; 
     } 
    } 

    // main thing is there, i have use the reflaction here.... 

    @Override 
    protected void onPostExecute(Object backresult) { 
     Method m; 
     try { 

      m = mContext.getClass().getDeclaredMethod(returnFunctionName, String.class); 
      m.invoke(mContext, (String) backresult); 
     } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } 
    } 

在主叫類

//call this class where you want and get dynamic response 
new AsyncTaskConnection(this).execute("getHomepage",jo.toString()); 

// and make response fuction 

protected void getHomepageResponse(String backresult) { 
      try { 
// this is your response 
       mainObject = new JSONObject(backresult); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

並且可以有多種途徑獲得結果是你想要的。

+0

沒關係,如果你喜歡一個單一的調用。我的網絡任務稍微複雜一些。不過,我會試着做出這樣的事情。問題是,我有20行代碼都依賴於網絡連接的響應,然後我無法在主線程中等待響應。你知道我的意思? – rst

+0

你究竟想要什麼@RobertStettler我沒有得到它。您能否詳細解釋一下 – Killer

+0

我也想知道 – Killer

1

是的,這是可能的,但你需要創建一些通用的AsyncTask,接收結果時需要API(url調用)和處理程序。

異步類:

public class AsyncTaskThread extends AsyncTask<Object, Object, Object> { 
    //instance variables 
    Handler myHandler; 
    public AsyncTaskThread(String uriString, Handler myHandler) { 
     //set these arguments to respective instance variables 
     this.myHandler=myHandler; 
    } 
     protected Object doInBackground(Object... obj){ 
      //business logic 
     } 
    //----implement other methods of Async as per your requirement 
     @Override 
    protected void onPostExecute(Object result) { 
      super.onPostExecute(result); 
      Message message = new Message(); 
      message.obj = result; 
      myHandler.sendMessage(message); 
} 

}

螺紋通處理器:
您需要通過處理器上面異步任務的說法。

new AsyncTaskThread(uri,new MyHandler()) 

import android.os.Handler; 
private class MyHandler extends Handler{ 
     @Override 
     public void handleMessage(Message msg) { 
      Model response = (Model) msg.obj; 
     } 
} 

當結果收到來自異步任務的傳遞處理程序調用sendMessage()方法。

我的建議:
如果您正在使用okhttp的API調用客戶端庫,請參閱https://github.com/square/okhttp/wiki/Recipes

+0

謝謝,我不明白//在實現方法的地位?我認爲你可以只有一個doinbackground,而不是? – rst

+0

我編輯了職位,我的意思是實施其他方法的異步類 – VVJ

+0

這不是我的問題:afaik你可以只有一個「doinbackground」方法每個類.... – rst

相關問題