android - JSON Object cannot be converted to JSONArray Search App -


i´d create search android app when try json value along ...json object cannot converted jsonarray. btw using volley.

code sample getting json:

   private void getdata() {      string id = edittextid.gettext().tostring().trim();      if (id.equals("")) {         toast.maketext(this, r.string.i_enter_request, toast.length_long).show();         return;     }      loading = progressdialog.show(this,getstring(r.string.d_wait),getstring(r.string.d_fetch),false,false);      string url = appconfig.data_url+edittextid.gettext().tostring().trim();      jsonarrayrequest searchreq = new jsonarrayrequest(url,             new response.listener<jsonarray>() {                 @override                 public void onresponse(jsonarray response) {                     log.d(tag, response.tostring());                     toast.maketext(getapplicationcontext(), "response: " + response.tostring(), toast.length_long).show();                     loading.dismiss();                      // parsing json                     (int = 0; < response.length(); i++) {                         try {                              jsonobject pobj = response.getjsonobject(i);                              products item = new products();                              item.settitle(pobj.getstring("name"));                              productitems.add(item);                          } catch (jsonexception e) {                             e.printstacktrace();                         }                      }                      adapter.notifydatasetchanged();                 }             }, new response.errorlistener() {         @override         public void onerrorresponse(volleyerror error) {             volleylog.d(tag, "error: " + error.getmessage());             toast.maketext(getapplicationcontext(), "error: " + error.getmessage(), toast.length_long).show();             loading.dismiss();          }     });      appcontroller.getinstance().addtorequestqueue(searchreq); } 

and json searching "1":

{     products: [     {         id: "1",         name: "test",         price: "123",         item_desc: "its test"     }],     success: 1 } 

it great if me it.

this not jsonarray:

{products:[{id: "1", name: "test", price: "123", item_desc: "its test" } ], success: 1 }

so you're getting error because callback tries passing in jsonarray need change callback to:

@override public void onresponse(jsonobject response) {} 

and need parse response jsonobject, not jsonarray.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -