Strangley, my AsyncTask will execute onPreExecute but not doInBackground() or onPostExecute().
Logcat has no error or message indicating that something is going wrong.
class LocationTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG, "onPreExecute============");
}
@Override
protected String doInBackground(String... urls) {
Log.d(TAG, "doInBackground============");
Log.d(TAG, "urls[0]:" + urls[0]);
return "test";
}
@Override
protected void onPostExecute(String result){
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d(TAG, "onPostExecute============");
}
}
public class CsLocationTaskTest extends AndroidTestCase {
public void testExecute(){
LocationTask task = new LocationTask();
task.execute("my_test_url", null);
}
}
As you see, AsycTask is nothing special, but doInBackground and onPostExecute are not being called in both a test case and in my Activity. I don’t understand what happened, please let me understand what is wrong. Thanks.
AsyncTaskneeds to be a sub class ofCsLocationTaskTest. Also, you don’t neednull.Per the development document:
"AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)"Found at: http://developer.android.com/reference/android/os/AsyncTask.html
Try this: