android - Show ProgressDialog while Using AsynTAsk to parse JSON and populate a list -


i have 2 asynctasks. in first 1 iam fetching data server in json form , converting string. in second 1 iam parsing string objects.

all happens after user clicks on button in alertdialog. while happening want show progressdialog. alertdialog dismisses after button click fine. progress dialog not show.

here code:-

private void poststring(string postedstring)             {                 // todo auto-generated method stub                  string posturl  = endpoints.posturl;                 try                  {                     string response = new post().execute(posturl).get();                     string getrequesturl= url                     string items = new fetchitems().execute(getrequesturl).get();                     arraylist<hashmap<string, string>> updatedpostlist = new getlist().execute(items).get(); 

}

private class post extends asynctask<string, string, string>     {             @override         protected string doinbackground(string... params)         {             // todo auto-generated method stub              httpresponse response =null;             string resultstring = "";             string myresponsebody = "" ;             // creating http client             httpclient httpclient = new defaulthttpclient();             // creating http post             httppost request = new httppost(params[0]);              list<namevaluepair> namevaluepair =namevaluepairs               try              {                 request.setentity(new urlencodedformentity(namevaluepair));                 response = httpclient.execute(request);                 if(response.getstatusline().getstatuscode()== 200)                 {                     httpentity entity = response.getentity();                     if (entity != null)                     {                          inputstream inputstream = entity.getcontent();                         myresponsebody = converttostring(inputstream);                     }                 }             }             catch(exception e)             {                 e.printstacktrace();             }             return myresponsebody;         }   private class fetchitems extends asynctask<string, string, string>      {         // todo auto-generated method stub          @override         protected string doinbackground(string... params)          {             // todo auto-generated method stub             httpresponse response =null;             string resultstring = "";             string myresponsebody = "" ;             // creating http client             httpclient httpclient = new defaulthttpclient();             // creating http post             httpget request = new httpget(params[0]);               try              {                 response = httpclient.execute(request);                 if(response.getstatusline().getstatuscode()== 200)                 {                     httpentity entity = response.getentity();                     if (entity != null)                     {                          inputstream inputstream = entity.getcontent();                         myresponsebody = converttostring(inputstream);                     }                 }             }             catch(exception e)             {             }             return myresponsebody;         }              @override         protected void onpostexecute(string result)          {             // todo auto-generated method stub             super.onpostexecute(result);              if(pd!=null && pd.isshowing())                 pd.dismiss();         }              @override         protected void onpreexecute()          {             // todo auto-generated method stub             super.onpreexecute();           }   private class getlist extends asynctask<string, string, arraylist<hashmap<string, string>>>     {           @override         protected arraylist<hashmap<string, string>> doinbackground(string... params)         {             // todo auto-generated method stub             myclist.clear();             try {                 jsonobject jsonresponse = new jsonobject(new string(params[0]));                 jsonarray mtusers = jsonresponse.getjsonarray("listofc");                 (int = 0; < mtusers.length(); i++)                 {                     jsonobject menuobject = mtusers.getjsonobject(i); //parsing                      map = new hashmap<string,string>();                     map.put(my items here)                      mycommentslist.add(map);                  }             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             collections.reverse(mylist);             return mylist;         }        }    case r.id.btnadd:             scrollnews.fullscroll(v.focus_down);             btnaddcomms.setpressed(true);              builder = new alertdialog.builder(newsdetails.this);             builder.settitle("post comment");             builder.seticon(r.drawable.post_comment_button);               final edittext input1 = new edittext(newsdetails.this);             linearlayout.layoutparams lp = new linearlayout.layoutparams(                     linearlayout.layoutparams.match_parent,                     linearlayout.layoutparams.match_parent);              input1.setlayoutparams(lp);             builder.setview(input1);               builder.setpositivebutton("post", new dialoginterface.onclicklistener()             {                  public void onclick(dialoginterface dialog, int id)                  {                      pd = new progressdialog(details.this);                     pd.setmessage("posting..");                     pd.show();                     pd.setcancelable(true);                      postedstring= input1.gettext().tostring();                     dialog.dismiss();                      if(postedstring.length()>0)                     {                         post(postedstring);                      }                     else                     {                         toast.maketext(details.this, "please enter text.", toast.length_long).show();                         input1.findfocus();                     }                      } 

create progressdialog instance variable.

pdialog = new progressdialog(this); pdialog.setmessage("fetching data"); 

inside onpreexecute() method of asynctask :

pdialog.show(); 

inside onpostexecute() method of asnyctask :

pdialog.dismiss(); 

note 1 : don't need multiple asynctasks, can complete whole fetch , parsing inside first asynctask alone, since you're calling 1 asynctask after other.

string response = new post().execute(posturl).get(); string getrequesturl= url; string items = new fetchitems().execute(getrequesturl).get(); arraylist<hashmap<string, string>> updatedpostlist = new getlist().execute(items).get(); 

note 2 : when asynctask.execute.get() on ui thread, ui blocked, not encouraged. so, call asynctask.execute() , show progress dialog while happens in background.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -