2012-02-14 12 views
16

Terrò questo più semplice che posso.Come posso recuperare i dati da AsyncTasks doInBackground()?

Ho un metodo nel mio livello di controllo che utilizza una classe CallServiceTask che si estende AsyncTask. Quando si chiama new CallServiceTask().execute(parameters)
Come recuperare i dati restituiti da doInBackground? Tutte le esercitazioni che ho trovato utilizzano la classe che estende AsyncTask direttamente dal loro Activity.
Il mio problema è un po 'più complesso di quello.
Tutto quello che voglio è prendere il Object[] restituito da doInBackground e impostarlo sui membri di dati privati ​​della mia classe RestClient.

CallServiceTask assomiglia a questo:

private class CallServiceTask extends AsyncTask<Object, Void, Object[]> 
{ 

    protected Object[] doInBackground(Object... params) 
    { 
     HttpUriRequest req = (HttpUriRequest) params[0]; 
     String url = (String) params[1]; 

     return executeRequest(req, url); 
    } 
} 

E mia classe RestClient assomiglia a questo:

public class RestClient 
{ 

private ArrayList <NameValuePair> params; 
private ArrayList <NameValuePair> headers; 

private JSONObject jsonData; 

private Object[] rtnData; 

private String url; 

private boolean connError; 

public int getResponseCode() { 
    return responseCode; 
} 

/** 
* 
* @return the result of whether the login was successful by looking at the response parameter of the JSON object. 
*/ 
public Boolean DidLoginSucceed() 
{ 
    // Will Crash on socket error 
     return ((JSONObject) rtnData[0]).optBoolean("response"); 
} 

public String GetToken() 
{ 
    return jsonData.optString("token"); 
} 

public RestClient(String url) 
{ 
    this.url = url; 
    params = new ArrayList<NameValuePair>(); 
    headers = new ArrayList<NameValuePair>(); 
    rtnData = new Object[]{ new JSONObject() , Boolean.TRUE }; 
} 

public void AddParam(String name, String value) 
{ 
    params.add(new BasicNameValuePair(name, value)); 
} 

public void AddHeader(String name, String value) 
{ 
    headers.add(new BasicNameValuePair(name, value)); 
} 

/** 
* This method will execute, call the service and instantiate the JSON Object through executeRequest(). 
* 
* @param method an enum defining which method you wish to execute. 
* @throws Exception 
*/ 
public void ExecuteCall(RequestMethod method) throws Exception 
{ 
    Object[] parameters = new Object[]{ new HttpGet() , new String("") }; 
    switch(method) { 
     case GET: 
     { 
      //add parameters 
      String combinedParams = ""; 
      if(!params.isEmpty()){ 
       combinedParams += "?"; 
       for(NameValuePair p : params) 
       { 
        String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue()); 
        if(combinedParams.length() > 1) 
        { 
         combinedParams += "&" + paramString; 
        } 
        else 
        { 
         combinedParams += paramString; 
        } 
       } 
      } 

      HttpGet request = new HttpGet(url + combinedParams); 

      //add headers 
      for(NameValuePair h : headers) 
      { 
       request.addHeader(h.getName(), h.getValue()); 
      } 
      parameters[0] = request; 
      parameters[1] = url; 

      new CallServiceTask().execute(parameters); 

      jsonData = ((JSONObject) rtnData[0]).optJSONObject("data"); 
      connError = (Boolean) rtnData[1]; 
      break; 

     } 
     case POST: 
     { 
      HttpPost request = new HttpPost(url); 

      //add headers 
      for(NameValuePair h : headers) 
      { 
       request.addHeader(h.getName(), h.getValue()); 
      } 

      if(!params.isEmpty()){ 
       request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
      } 
      new CallServiceTask().execute(request, url); 
      break; 
     } 
    } 
} 

private Object[] executeRequest(HttpUriRequest request, String url) 
{ 
    HttpClient client = new DefaultHttpClient(); 
    client = getNewHttpClient(); 

    HttpResponse httpResponse; 

    try { 
     httpResponse = client.execute(request); 
     HttpEntity entity = httpResponse.getEntity(); 

     if (entity != null) { 

      InputStream instream = entity.getContent(); 
      String response = convertStreamToString(instream); 
      try { 
       rtnData[0] = new JSONObject(response); 
       rtnData[1] = false; 

      } catch (JSONException e1) { 
       rtnData[1] = true; 
       e1.printStackTrace(); 
      } 

      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 

    } catch (ClientProtocolException e) { 
     client.getConnectionManager().shutdown(); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     client.getConnectionManager().shutdown(); 
     e.printStackTrace(); 
    } 
    return rtnData; 
} 


private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

/** 
* Custom HTTP Client accepting all SSL Certified Web Services. 
* 
* @return n HttpClient object. 
*/ 
public HttpClient getNewHttpClient() { 
    try { 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     trustStore.load(null, null); 

     SSLSocketFactory sf = new MySSLSocketFactory(trustStore); 
     sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

     HttpParams params = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     registry.register(new Scheme("https", sf, 443)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

     return new DefaultHttpClient(ccm, params); 
    } catch (Exception e) { 
     return new DefaultHttpClient(); 
    } 
} 

risposta

24

L'unico modo per farlo è utilizzare un CallBack. Si può fare qualcosa di simile:

new CallServiceTask(this).execute(request, url); 

Poi, nel tuo CallServiceTask aggiungere una variabile di classe locale e chiamare un metodo da quella classe nella vostra OnPostExecute:

private class CallServiceTask extends AsyncTask<Object, Void, Object[]> 
{ 
    RestClient caller; 

    CallServiceTask(RestClient caller) { 
     this.caller = caller; 
    } 


    protected Object[] doInBackground(Object... params) 
    { 
     HttpUriRequest req = (HttpUriRequest) params[0]; 
     String url = (String) params[1]; 
     return executeRequest(req, url); 
    } 

    protected onPostExecute(Object result) { 
     caller.onBackgroundTaskCompleted(result); 
    } 
} 

Poi è sufficiente utilizzare l'oggetto come ti piace in il metodo onBackgroundTaskCompleted() nella classe RestClient.

Una soluzione più elegante ed estendibile sarebbe utilizzare le interfacce. Per un'implementazione di esempio, consultare la libreria this. L'ho appena iniziato ma ha un esempio di ciò che vuoi.

8

È possibile utilizzare get() per recuperare la value/object ritorno da AsyncTask.

new CallServiceTask().execute(parameters).get(); 

Ciò restituirà si computed risultato che si sta tornando. Ma questo bloccherà la tua interfaccia utente fino al completamento del tuo processo in background.

Un'altra opzione è creare un'interfaccia o un BroadcastReceiver per restituire il valore non appena è stato completato il numero doInBackground(). Avevo creato una demo per lo stesso utilizzando Interface e BroadcastReceiver è possibile verificare se da my github

Inoltre, utilizzando il secondo approccio l'interfaccia utente non verrà bloccata!

+3

Ma get() causa anche la mia domanda di fermarsi e aspettare fino a quando il metodo restituisce in realtà e dal momento che l'intero il punto di utilizzo di AsyncTask era di rendere la chiamata al servizio web asincrona. – CodePrimate

+1

Non seguo. Ho il mio LoginController che viene utilizzato dalla mia attività quando l'utente preme il pulsante Accedi. LoginController crea quindi un'istanza di RestClient e chiama RestCLient.ExecuteCall. ExecuteCall aggiunge i parametri all'url e quindi chiama rtnData = new CallServiceTask(). Execute (parameters) .get(); In che modo questo non farà sedere l'attività e aspetterà che get() torni? – CodePrimate

+0

Un'altra cosa che puoi fare è usare il risultato calcolato nel metodo 'onPostExecute()' per fare qualsiasi materiale dell'interfaccia utente. –

11

come @ saad-farooq menzionato è possibile utilizzare interface per quello. Quindi è possibile utilizzare il Handler.Callback o definire il proprio:

public interface ClientIF { 

    public void onResponseReceived(Object result); 

} 

allora avete bisogno di implementare nel vostro CallServiceTask

public abstract class CallServiceTask extends AsyncTask<Object, Void, Object[]> 
    implements ClientIF 
{ 
    Activity activity; 

    CallServiceTask(Activity activity) { 
     this.activity = activity; 
    } 

    public abstract void onResponseReceived(Object result); 

    protected Object[] doInBackground(Object... params) 
    { 
     HttpUriRequest req = (HttpUriRequest) params[0]; 
     String url = (String) params[1]; 
     return executeRequest(req, url); 
    } 

    protected onPostExecute(Object result) { 
     onResponseReceived(result); 
    } 
} 

atto che il costructor viene modificata in modo da poter chiamare da ogni Activity classe. poi fare l'istanza di questa classe nel vostro RestClient

public class RestClient 
{ 
CallServiceTask service = new CallServiceTask() { 
    @Override 
    public void onResponseReceived(Object result) { 
    // TODO Auto-generated method stub 

    } 
}; 

} 
1
public class getOperators extends AsyncTask<Void, Void, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     items = new ArrayList(); 
     dialog = new ProgressDialog(PrepaidOperatorsListActivity.this); 
     dialog.setMessage("Please Wait..."); 
     dialog.setCancelable(false); 
     dialog.show(); 
    } 



    @Override 
    protected String doInBackground(Void... params) { 
     BufferedReader reader; 
     StringBuffer buffer; 
     String res = null; 

     try { 
      URL url = new URL(""); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      con.setReadTimeout(40000); 
      con.setConnectTimeout(40000); 
      con.setRequestMethod("GET"); 
      con.setRequestProperty("Content-Type", "application/json"); 
      int status = con.getResponseCode(); 
      InputStream inputStream; 
      if (status == HttpURLConnection.HTTP_OK) { 
       inputStream = con.getInputStream(); 
      } else { 
       inputStream = con.getErrorStream(); 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 
      buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 
      res = buffer.toString(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return res; 
    } 
    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     System.out.println("JSON RESP:" + s); 
     String response = s; 
     try { 

      JSONObject ecomerrce = new JSONObject(response); 
      JSONArray jsonArray = ecomerrce.getJSONArray("prepaid_operators"); 

      for (int j = 0; j < jsonArray.length(); j++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(j); 

       PrepaidOperatorsPojo prepaidOperatorsPojo = new PrepaidOperatorsPojo(jsonObject.getString("operator_name"), jsonObject.getString("operator_code"), jsonObject.getString("operator_display_comission"), jsonObject.getString("operator_calculate_comission")); 
       items.add(prepaidOperatorsPojo); 

      } 
      if (items.size() > 0) { 
       dialog.dismiss(); 
       prepaidOperatorListAdapter = new PrepaidOperatorListAdapter(PrepaidOperatorsListActivity.this, items); 
       rvPrepaidOperatorList.setAdapter(prepaidOperatorListAdapter); 

      } else { 
       dialog.dismiss(); 
       Toast.makeText(PrepaidOperatorsListActivity.this, "No Data to display", Toast.LENGTH_SHORT).show(); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
1
private void walletStatements() { 
     JSONObject post_search = new JSONObject(); 
     try { 

      post_search.put("username", getUserName); 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     if (post_search.length() > 0) { 
      try { 
       new Getwalletstatements().execute(String.valueOf(post_search)); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    } 


    class Getwalletstatements extends AsyncTask<String, String, String> { 
     String JsonResponse = null; 
     ProgressDialog dialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      dialog = new ProgressDialog(ReportsActivity.this); 
      dialog.setMessage("Please Wait..."); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String JsonDATA = params[0]; 
      HttpURLConnection urlConnection = null; 
      BufferedReader reader = null; 
      URL url = null; 

      try { 
       url = new URL(Constant.url+"GetReports_v9.php"); 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setDoOutput(true);    // is output buffer writter 
       urlConnection.setRequestMethod("POST"); 
       urlConnection.setRequestProperty("Content-Type", "application/json"); 
       urlConnection.setRequestProperty("Accept", "application/json");//set headers and method 
       Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8")); 
       writer.write(JsonDATA);// json data 
       writer.close(); 

       InputStream inputStream = urlConnection.getInputStream();//input stream 
       StringBuffer buffer = new StringBuffer(); 
       if (inputStream == null) {     // Nothing to do. 
        return null; 
       } 
       reader = new BufferedReader(new InputStreamReader(inputStream)); 
       String inputLine; 

       while ((inputLine = reader.readLine()) != null) 
        buffer.append(inputLine + "\n"); 

       if (buffer.length() == 0) {     // Stream was empty. No point in parsing. 
        return null; 
       } 

       JsonResponse = buffer.toString(); 


       return JsonResponse; 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (ProtocolException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
       if (reader != null) { 
        try { 
         reader.close(); 
        } catch (final IOException e) { 

        } 
       } 
      } 


      return null; 
     } 

     @Override 
     protected void onPostExecute(String s) { 

      if (JsonResponse != null) { 
       try { 
        NetworkIN.iv= Constant.iv; 
        NetworkIN.change=Constant.key; 
        NetworkIN mCrypt=new NetworkIN(); 
        String decrypted = new String(mCrypt.decrypt(JsonResponse.trim())); 

        if(decrypted!=null){ 
         JSONObject ordersHistory = new JSONObject(decrypted); 
         msg = ordersHistory.getString("msg"); 

         JSONArray jsonArray = ordersHistory.getJSONArray("PPDetails"); 

         ordersCount = jsonArray.length(); 
         //for (int j = 0; j < jsonArray.length(); j++) 
         for (int j = jsonArray.length() - 1; j >= 0; j--) 
         { 
          JSONObject jsonObject = jsonArray.getJSONObject(j); 


          String message,total_in,inType ; 

          total_in =jsonObject.getString("total_in"); 
          inType =jsonObject.getString("in_type"); 
          message="Congratulations your wallet is credited by Rs."+total_in+" because of "+inType; 
          ReportsPojo reportsPojo = new ReportsPojo(jsonObject.getString("reg_id"), 
            jsonObject.getString("username"), 
            jsonObject.getString("transfer_from"), 
            jsonObject.getString("transfer_to"), 
            jsonObject.getString("total_in"), 
            jsonObject.getString("tds"), 
            jsonObject.getString("in_amount") , 
            jsonObject.getString("out_amount"), 
            jsonObject.getString("in_type"), 
            jsonObject.getString("created_on"), 
            message); 

          reportsItems.add(reportsPojo); 
         } 
        }else{ 

        } 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       if (msg.equals("Success")) { 

        reportsAdapter = new ReportsAdapter(ReportsActivity.this, reportsItems); 
        reportsListview.setAdapter(reportsAdapter); 

       } else { 
        Toast.makeText(ReportsActivity.this, "Sorry "+msg, Toast.LENGTH_LONG).show(); 

       } 
       dialog.dismiss(); 
      } else { 
       dialog.dismiss(); 
      } 
     } 
0
public class MyAsyncTask extends AsyncTask<String, Void, String> { 

    @Override  
    protected void onPreExecute() { 
     //showProgressDialog 
     dialog = new ProgressDialog(this); 
     dialog.setMessage("Loading........"); 
     dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     dialog.show(); 
    } 

    @Override  
    protected String doInBackground(String... strings) { 
     HttpURLConnection httpURLConnection; 
     BufferedReader reader; 
     int responseCode ; 

     try { 
      URL url = new URL(YOUR_URL); 
      URLConnection urlConnection = url.openConnection(); 
      httpURLConnection = (HttpURLConnection) urlConnection; 
      httpURLConnection.setRequestMethod("GET"); 
      httpURLConnection.setDoOutput(true); 

      responseCode = httpURLConnection.getResponseCode(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      if (inputStream != null) { 
       if (responseCode == 200) { 
        reader = new BufferedReader(new InputStreamReader(inputStream)); 
        StringBuilder buffer = new StringBuilder(); 
        String line; 
        while ((line = reader.readLine()) != null) { 
         buffer.append(line); 
        } 
        return buffer.toString(); 
       } 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override  
    protected void onPostExecute(String result) { 
     progressDialog.dismiss(); 
     // Fetch Your Data Add Your Code Here 
    } 
} 
Problemi correlati