Search code examples
androidandroid-asynctask

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");'


Solution

  • 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();
        }
    }