Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask ; doInbackground() not called after calling method onPreExecute()

I did added async library at my project and checked already, I don't know why code flow doesn't go in to asynctask

Code

public void doMysql() {     Log.v("doMysql", "accessed");      new AsyncTask<Void, Void, String>() {         @Override         protected void onPreExecute() {             super.onPreExecute();             Log.e("AsyncTask", "onPreExecute");         }         @Override         protected String doInBackground(Void... params) {             Log.v("AsyncTask", "doInBackground");              String msg = "";              DefaultHttpClient httpclient = new DefaultHttpClient();             HttpPost httppost = new HttpPost("http://172.16.100.172:52273/mysql");              ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();              nameValuePairs.add(new BasicNameValuePair("myday", Integer.toString(day_picker.getYear()) +                      addZero(day_picker.getMonth() + 1) +                      addZero(day_picker.getDayOfMonth())));             nameValuePairs.add(new BasicNameValuePair("mystar", changeStar(day_picker.getMonth() + 1, day_picker.getDayOfMonth())));             nameValuePairs.add(new BasicNameValuePair("mybt", changeBloodType(blood_picker.getValue())));             nameValuePairs.add(new BasicNameValuePair("mynum", "" + myPhone.getText()));             nameValuePairs.add(new BasicNameValuePair("yournum", "" + partnerPhone.getText()));             nameValuePairs.add(new BasicNameValuePair("myregID", regid));             try {                 Log.v("setEntity", "before");                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                 Log.v("setEntity", "after");              } catch (UnsupportedEncodingException e1) {                 Log.v("UnsupportedEncodingException", "");                 e1.printStackTrace();             }             //172.16.101.28             try {                   Log.v("post", "before");                 HttpResponse httpresponse = httpclient.execute(httppost);                 Log.v("post", "after");                 Log.v("HttpResponse ",httpresponse.getEntity().toString());             } catch (ClientProtocolException e) {                 Log.v("ClientProtocolException", "ClientProtocolException");                 e.printStackTrace();             } catch (IOException e) {                 Log.v("IOException", "IOException");                  e.printStackTrace();             }              return msg;         }         @Override         protected void onPostExecute(String msg) {             Log.v("AsyncTask", "onPostExecute");          }     }.execute(null, null, null); } 

I have a log statement in the code 'Log.v("AsyncTask", "doInBackground");'

But it doesn't show up in the logger 'Log.v("AsyncTask", "doInBackground");'

like image 935
LKM Avatar asked Nov 12 '14 15:11

LKM


People also ask

How to use AsyncTask in java?

AsyncTask Example The following code snippet must be present in the MainActivity class in order to launch an AsyncTask. MyTask myTask = new MyTask(); myTask. execute(); The execute method is used to start the background thread in the excerpt above, where we've used a sample class name that extends AsyncTask.

How to call AsyncTask class in Android?

AsyncTask class is firstly executed using execute() method. In the first step AsyncTask is called onPreExecute() then onPreExecute() calls doInBackground() for background processes and then doInBackground() calls onPostExecute() method to update the UI.

What is the use of AsyncTask in Android Studio?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.

What is Async task in java?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .


2 Answers

You should execute your task with executor

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working.

In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor.

Change your code to

public void executeAsyncTask() {     AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {         @Override         protected void onPreExecute() {             super.onPreExecute();             Log.e("AsyncTask", "onPreExecute");         }          @Override         protected String doInBackground(Void... params) {             Log.v("AsyncTask", "doInBackground");             String msg = null;             // some calculation logic of msg variable             return msg;         }         @Override         protected void onPostExecute(String msg) {             Log.v("AsyncTask", "onPostExecute");         }     };      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB/*HONEYCOMB = 11*/) {         task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);     } else {         task.execute();     } } 
like image 168
Sergey Shustikov Avatar answered Sep 23 '22 03:09

Sergey Shustikov


Doing this not working for you?

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

I was already doing this and the problem was still there, the problem was I was using AsyncTask to run long background tasks that do run forever, so the ThreadPoolExecutor's worker threads were already occupied.

In my case AsyncTask.THREAD_POOL_EXECUTOR (use debugger to get your executor properties) was having the ff properties:

maximumPoolSize = 3 largetsPoolSize = 2 corePoolSize =2  

So disabling one of the long running asyncTasks made my doInBackground code to run but that is not the right solution.

The right solution is I moved long running tasks into custom threads rather than using AsyncTask.

like image 27
dsharew Avatar answered Sep 23 '22 03:09

dsharew