android - ProgressDialog getting Dismissed before Toast appears from doInBackground method of AsyncTask -
i'm making json http
request using asynctask
. have created progressdialog
in onpreexecute()
, i'm dismissing in onpostexecute()
. problem is, there toast message in diinbackground()
which shows json response
, progressdialog
being dismissed before toast
appears, meaning onpostexecute()
being called before toast
generated. how can dismiss before toast
appears?
here 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 write volley request in async task. can directly excute task because internaly it's using thread. once got response or failure u can close dialog there self. guess helps
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) { if(progressdialog.isshowing){ progressdialog.cancel(); } 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()); if(progressdialog.isshowing){ progressdialog.cancel(); } } }); appcontroller.getinstance().addtorequestqueue(req);
i hope helps , no need of asynctask.
Comments
Post a Comment