1

I am fetching and processing some JSON data in a background thread...even then I can see warning in my log saying that the application is doing too much work in ht ui thread. I cannot understand what is wrong.

Here is my method:

     private void getLists1() {
            busy =true;
            runOnUiThread(()->{
                listPD.show();
            });
            MyAsyncTask1 asyncTask1 = new MyAsyncTask1((String output) -> {
                if (!output.equals("failed")) {
                    runOnUiThread(()->{
                        listPD.setMessage("getting expense list");
                    });
                    JSONObject jsonObject = new JSONObject(output);
                    JSONArray array = jsonObject.getJSONArray("records_et");
                    int ii = array.length();
                    for (int i = 0; i < ii; i++) {
                        JSONObject jsonObject1 = array.getJSONObject(i);
                        String id = jsonObject1.getString("id");
                        String name1 = jsonObject1.getString("name");
                        int finalI = i;

                        listDB.insertItem(name1, id);

                    }
                    runOnUiThread(()->{
                        listPD.setMessage("expense list saved");
                    });

                }
            });
            asyncTask1.executeOnExecutor((AsyncTask.THREAD_POOL_EXECUTOR), "http://webservice");
            MyAsyncTask1 asyncTask2 = new MyAsyncTask1(output1 -> {
                if (!output1.equals("failed")) {
                    Log.i("response", "vendor " + output1);
                    runOnUiThread(()->{
                        listPD.setMessage("getting vendor list");
                    });
                    //vendor list
                    JSONObject jsonObject12 = new JSONObject(output1);
                    JSONArray array1 = jsonObject12.getJSONArray("records_vendor");
                    int ii1 = array1.length();

                    for (int i = 0; i < ii1; i++) {
                        JSONObject jsonObject1 = array1.getJSONObject(i);
                        String id = jsonObject1.getString("id");
                        String name1 = jsonObject1.getString("name");
                        int finalI = i;

                        listDB.insertVendorItem(name1, id);
                    }
                    runOnUiThread(()->{
                        listPD.setMessage("vendor list saved");
listPD.dismiss();
                    });

                }
            });
            asyncTask2.executeOnExecutor((AsyncTask.THREAD_POOL_EXECUTOR), "http://webservice");
    }

I call the method like this;

new Thread(()->{
getLists1();
}).start();

listPD is the progress dialogue...the app starts lagging when I call this function what I am I missing here.

  • 1
    Why do you have a thread starting off 2 async tasks? That's really weird. Post your MyAsyncTask class. The problem may be there. I don't think you understandAsyncTasks, since there's no need to make a Thread to start them, and you would never have a runOnUiThread as the last line of an AsyncTask- that's what onPostExecute is for. – Gabe Sechan Oct 01 '18 at 06:44
  • I am using one asynctask from a different class and I am implementing an interface that is called onPostExecure ,so I can reuse the code. I am also executing it on a ThreadPoolExecutor that allows parallel execution of asynctasks. The problem is that the background work is affecting the ui thread. I just noticed this code works perfectly in my emulator, but lags on my old HTC with 1gb of ram maybe the code is correct just the huge size of the JSON is causing problems... –  Oct 01 '18 at 07:17
  • I will try this with GSON or Jackson for better performance.. do you know how the same can be done using either of the two? –  Oct 01 '18 at 07:17

1 Answers1

1

AsyncTask class is used to do background operations that will update the UI(user interface). Mainly we used it for short operations that will not effect on our main thread.

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. Follow this syntax to use AsyncTask

Syntax of AsyncTask In Android:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
    // code that will run in the background
    return ;
}

protected void onProgressUpdate(Integer... progress) {
    // receive progress updates from doInBackground
}

protected void onPostExecute(Long result) {
    // update the UI after background processes completes
}

}

Executions of AsyncTask class from main thread:

new DownloadFilesTask().execute(url1, url2, url3);
Amit Nayak
  • 583
  • 1
  • 6
  • 17
  • I am using one asynctask from a different class and I am implementing an interface that is called onPostExecure , so I can reuse the code. I am also executing it on a ThreadPoolExecutor that allows parallel execution of asynctasks. The problem is that the background work is affecting the ui thread. I just noticed this code works perfectly in my emulator, but lags on my old HTC with 1gb of ram maybe the code is correct just the huge size of the JSON is causing problems... –  Oct 01 '18 at 07:16
  • I will try this with GSON or Jackson for better performance.. do you know how the same can be done using either of the two? –  Oct 01 '18 at 07:18
  • 2
    In constructor pass GSON data : public DownloadFilesTask(GSON data){} and call execute() from any other class you want like : new DownloadFilesTask(GsonData).execute(url1, url2, url3); – Amit Nayak Oct 01 '18 at 07:32