Android Exception while parsing JSON URL -
i'm trying parse json response url , populate array. exception when trying parse json response url:
org.json.jsonexception: value <!doctype of type java.lang.string cannot converted jsonarray
here's response looks like:
{"firstname":"roger","lastname":"davis","email":"rog@gmail.com","password":"test123","profilepicture":"googleimagesblablabla","settings":[]}
my code:
url url = new url(myurl); httpurlconnection urlrequest = (httpurlconnection) url.openconnection(); urlrequest.setrequestmethod("get"); urlrequest.addrequestproperty("content-type", "application/json"); urlrequest.addrequestproperty("accept", "application/json"); urlrequest.addrequestproperty("x-zumo-application", mobileserviceappid); urlrequest.addrequestproperty("email",email); urlrequest.addrequestproperty("password",pass); jsonobject[] response = null; try { // prepare objects receive bytes of data // azure mobile service inputstream in = new bufferedinputstream( urlrequest.getinputstream()); bufferedreader bufferreader = new bufferedreader( new inputstreamreader(in)); // responsestring hold our json data stringbuilder responsestring = new stringbuilder(); string line; // loop through buffered input, reading json data while ((line = bufferreader.readline()) != null) { responsestring.append(line); } // convert responsestring jsonarray jsonarray jsonarray = new jsonarray(responsestring.tostring()); toast.maketext(getapplicationcontext(),jsonarray.length(),toast.length_long).show(); for(int i=0;i<jsonarray.length();i++) { jsonobject obj = jsonarray.getjsonobject(i); log.d("error",obj.getstring("lastname")); } } catch(exception ex) { log.d("error", ex.tostring()); }
am parsing values wrong response? how can fix this, folks?
well json response not have array in :
{"firstname":"roger","lastname":"davis","email":"rog@gmail.com","password":"test123","profilepicture":"googleimagesblablabla"}
it has object in response can parse response below given code snippet.
jsonobject reader = new jsonobject(responsestring.tostring()); string firstname = reader.getstring("firstname"); string lastname= reader.getstring("lastname"); string email= reader.getstring("email"); string password= reader.getstring("password"); string profilepicture= reader.getstring("profilepicture");
and remove part code :
// convert responsestring jsonarray jsonarray jsonarray = new jsonarray(responsestring.tostring()); toast.maketext(getapplicationcontext(),jsonarray.length(),toast.length_long).show(); for(int i=0;i<jsonarray.length();i++) { jsonobject obj = jsonarray.getjsonobject(i); log.d("error",obj.getstring("lastname")); }
Comments
Post a Comment