Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon class for AsyncTask in Android?
    text
    copied!<p>I have a common class say for eg Class A which extends <code>AsyncTask</code> and has all the methods implemented i.e. <code>onPreExecute</code>, <code>doinbackground</code> and <code>onPostExecute</code>.</p> <p>Now, there are other classes which want to use Class A object.</p> <p>Say Class B uses class A in the below manner</p> <pre><code>A a = new A(context) a.execute(url) </code></pre> <p>Then i fetch the result in get method. But get method is not the proper way of using AsyncTask. I will like to get the result in <code>onPostExecute</code>. For that i tried using a boolean parameter which will get true only in <code>onpostexecute</code>. The class B will check till it gets true and when it gets true it will fetch the result.</p> <p>But this is somehow blocking the application.</p> <p>I have placed the code for asynctask below.</p> <p>'</p> <pre><code>import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; public class A extends AsyncTask&lt;String, Void, String&gt; { private Context context = null; private final HttpClient httpClient = new DefaultHttpClient(); private String content = null; //private String error = null; private String finalResult = null; private static boolean isResult = false; private ProgressDialog progressDialog = null; public BabbleVilleSyncTask(Context context) { this.context = context; progressDialog = new ProgressDialog(this.context); } protected void onPreExecute() { progressDialog.setMessage("Please Wait...."); progressDialog.show(); } protected String doInBackground(String... urls) { try { //urls[0] = URLEncoder.encode(urls[0], "UTF-8"); HttpGet httpget = new HttpGet(urls[0]); ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler(); content = httpClient.execute(httpget, responseHandler); } /*catch(UnsupportedEncodingException ue) { error = ue.getMessage(); }*/ catch (ClientProtocolException e) { //error = e.getMessage(); cancel(true); } catch (IOException e) { //error = e.getMessage(); cancel(true); } httpClient.getConnectionManager().shutdown(); return content; } protected void onPostExecute(String result) { finalResult = result; progressDialog.dismiss(); System.out.println("on Post execute called"); isResult = true; } public boolean getIsResult() { return isResult; } public void setIsResult(boolean flag) { isResult = flag; } public String getResult() { return finalResult; } } </code></pre> <p>'</p> <p>Can someone let me know what the issue may be?</p> <p>Regards</p> <p>Sunil</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload