android - Make asynchronous http calls synchronous while displaying progress bar? -
i want json web service ,displaying progress circle , processing , displaying data. how make asynchronous calls synchronous without jdts , loading circle ?any library or else?
for example
....some code call json webservice //wait http call execute while displaying loading circle update ui
well, 1 solution using progressdialog, other progressbar, progressdialog send dialog progress circle.
for progressdialog should know:
progressdialog = new progressdialog(mainactivity.this); progressdialog.settitle("downloading json ..."); progressdialog.setmessage("download in progress ..."); progressdialog.setprogressstyle(progressdialog.style_horizontal);//horizontal progressdialog.setprogressstyle(progressdialog.style_spinner); //circular progressdialog.setprogress(0); progressdialog.setmax(20); // progress has 20 steps progressdialog.setindeterminate(true);// infinite loop in progress progressdialog.show();
for asynctask should know:
doinbackground() there source code asynchronous process.
onprogressupdate() there source code send pieces main thread include ui things. works should use publishprogress() inside doinbackground() method.
onpostexecute() method executed after doinbackground finish process
onpreexecute() method initialize things need in asynctask
you can declare , instantiate progressdialog @ beginning on activity, set progressdialog.show() in asynctask inside onpreexecute()
private class jsonwebservice extends asynctask<url, integer, jsonobject> { protected void onpreexecute(){ progressdialog.show(); } protected jsonobject doinbackground(url... urls) { //here code download json // //publishprogress(counter); return json; } protected void onprogressupdate(integer... progress) { setprogresspercent(progress[0]); } protected void onpostexecute(jsonobject result) { //use json want download finished // close progressdialog progressdialog.dismiss(); } }
Comments
Post a Comment