Abhishek Veenakkat Abhishek Veenakkat - 30 days ago 11
Java Question

async task not running when called from other async task

class TestAsync extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
v.findViewById(R.id.loadinglayout).setVisibility(View.VISIBLE);
v.findViewById(R.id.wholecontentlayout).setVisibility(View.INVISIBLE);
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... voids) {
callAPI();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
v.findViewById(R.id.loadinglayout).setVisibility(View.INVISIBLE);
v.findViewById(R.id.wholecontentlayout).setVisibility(View.VISIBLE);
}}



public void callAPI ()
{
RequestInterface requestInterface = new RequestImplementation();
requestInterface.setUrl("https://api.myjson.com/bins/vl9pp");
ConnectionExecutor connectionExecutor = new ConnectionImplementation();
ResponseInterface temp = null;
try {
temp = connectionExecutor.executeConnection(requestInterface);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
rangeText.setText(temp.getPayload());}


Call API function will can method of another class where HttpConnection is done.

If function is directly called from onViewCreated() in fragment then it works. If I call it from another async task it returns nothing.

I am actually trying to show a progressbar when the callApi function is called.

k3b k3b
Answer Source

It is not allowed to start an AsyncTask outside the UI thread. doInBackground is outside the UI thread.

From https://developer.android.com/reference/android/os/AsyncTask.html:

Threading rules

...

The AsyncTask class must be loaded on the UI thread.