AsyncTask does not work asynchronously

The following should have been the same, if I'm not mistaken.
Using AsyncTask :

private class GetDataTask extends AsyncTask<String, Void, String>{

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... params) {
        return NetConnection.getRecordData(mUserId, mUserPassword);
    }

    @Override
    protected void onPostExecute(String result) {
        parseJson(result);
    }
}

Usage Subject :

    new Thread( new Runnable() {

        @Override
        public void run() {
            String res = NetConnection. getRecordData(mUserId, mUserPassword);
            parseJson(res);

        }
    }).start();

But when the file is downloaded, AsyncTask launches synchronously, and Thread launches asynchronously(in parallel).
Why is that? Why is AsyncTask behaving like this? Is AsyncTask supposed to work asynchronously?
I'm a little confused, so I need your help. This is how I call GetDataTask:

new GetDataTask().execute()

I prefer to use AsyncTask, but this does not work for me. See the earlier question for more information.

+5
source share
4 answers

4.x 2 AsyncTasks .

-

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
  myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else {
  myTask.execute();
}

: http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html

+10

java-...

AsyncTasks . DONUT, , . HONEYCOMB, , , .

If you truly want parallel execution, you can invoke 

executeOnExecutor (java.util.concurrent.Executor, Object []) THREAD_POOL_EXECUTOR.

SO, AsyncTask.. ( - , encliar gingerbread)... executeOnExecutor parallel...

+3

, parseJson(result); onPostExecute() AsyncTask, MainUIThread . .

parseJson(result); doInBackGround(), .

Thread. ,

String res = NetConnection. getRecordData(mUserId, mUserPassword);
parseJson(res);

MAinUiThread, .

:

, parseJson(res); , doInBackground().

+2
source

It seems that the actual problem is not in downloading the file, but in the method parseJson.

In your example, Threadyou parse Json in a separate thread, and in the case AsyncTaskyou parse Json in a user interface thread.

0
source

All Articles