I'm making a JSON HTTP
request using AsyncTask
. I have created a ProgressDialog
in onPreExecute()
and i'm dismissing it in onPostExecute()
. The problem is, there is a Toast message in diInBackground()
which shows the JSON response
and ProgressDialog
is being dismissed before the Toast
appears, meaning onPostExecute()
is being called before the Toast
is generated. How can I dismiss it just before the Toast
appears?
Here is my code.
private class login extends AsyncTask<Void,Void,Void>{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
// Create & Start Progress Dialog
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
JsonObjectRequest req = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("from on error response Server Error..: ", error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(req);
return null;
}
@Override
protected void onPostExecute(Void v){
// Terminate Progress Dialog
progressDialog.dismiss();
}
}
No Need to write the Volley Request in Async Task. You can Directly Excute that task because internaly it's using thread. So Once you got response or Failure so u can close the dialog there it self. I guess it will helps you
I hope this will helps you And no need of AsyncTask.