java - How to get Json response -


im new android.. want response restful webservice. webservice works fine. but, how response android.. if run application please wait progress came... please

here code

 public void invokews(requestparams params){              // show progress dialog              prgdialog.show();              // make restful webservice call using asynchttpclient object              asynchttpclient client = new asynchttpclient();              client.get("http://192.168.2.2:9999/useraccount/login/dologin", params, new jsonhttpresponsehandler() {                  // when response returned rest has http response code '200'                  @override                  public void onsuccess(int statuscode, header[] headers, string response) {                      // hide progress dialog                      prgdialog.hide();                      try {                          // json object                          jsonobject obj = new jsonobject(response);                          // when json response has status boolean value assigned true                          if (obj.getboolean("status")) {                              toast.maketext(getactivity().getapplicationcontext(), "you logged in!", toast.length_long).show();                              // navigate home screen                              //navigatetohomeactivity();                          }                          // else display error message                          else {                              errormsg.settext(obj.getstring("error_msg"));                              toast.maketext(getactivity().getapplicationcontext(), obj.getstring("error_msg"), toast.length_long).show();                          }                      } catch (jsonexception e) {                          // todo auto-generated catch block                          toast.maketext(getactivity().getapplicationcontext(), "error occured [server's json response might invalid]!", toast.length_long).show();                          e.printstacktrace();                        }                  }                    // when response returned rest has http response code other '200'                  @override                  public void onfailure(int statuscode, header[] headers, string content, throwable error) {                      // hide progress dialog                      prgdialog.hide();                      // when http response code '404'                      if (statuscode == 404) {                          toast.maketext(getactivity().getapplicationcontext(), "requested resource not found", toast.length_long).show();                      }                      // when http response code '500'                      else if (statuscode == 500) {                          toast.maketext(getactivity().getapplicationcontext(), "something went wrong @ server end", toast.length_long).show();                      }                      // when http response code other 404, 500                      else {                          toast.maketext(getactivity().getapplicationcontext(), "unexpected error occcured! [most common error: device might not connected internet or remote server not , running]", toast.length_long).show();                      }                    }              });          }

in above code, header[] automatically strike out in onsuccess method. how fix it. please me!!

first of make sure have permission on manifest:

<uses-permission android:name="android.permission.internet" /> 

now second suggest use volley request. it's easy use , in case you're using android studio it's easier plugin library. add line app.gradle dependencies , you're go.

compile 'com.mcxiaoke.volley:library:1.0.17' 

for more information mirror library click here

now sample implementation:

public void sendrequest(view view){         progressdialog.setmessage("please wait");         progressdialog.show();          string url ="your_url";          // request string response provided url.         stringrequest stringrequest = new stringrequest(request.method.get, url,                 new response.listener<string>() {                     @override                     public void onresponse(string response) {                         log.e("message", "response is: " + response);                         progressdialog.dismiss();                      }                 }, new response.errorlistener() {             @override             public void onerrorresponse(volleyerror error) {                 log.e("message", "error");                 error.printstacktrace();                 progressdialog.dismiss();             }         });          stringrequest.settag("your_tag");         // add request requestqueue.         queue.add(stringrequest);     } 

and cancel request

public void cancelrequest(view view){         queue.cancelall("your_tag");         log.e("message", "request cancelled");     } 

for easier handling in case need change fragments displayed add on pause or on stop. goodluck!


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -