android - jsonException:End of input at character 1 of -
im making listview , using jsonobject array purposes im getting error dont know how fix dont know exception takes place when run program end , doesnt not show result java file hope can find exception
@override protected arraylist<todaydetails> doinbackground(void... params) { try { // create new http client defaulthttpclient defaultclient = new defaulthttpclient(); // setup request httpget httpgetrequest = new httpget("http://api.androidhive.info/contacts"); // execute request in client httpresponse httpresponse = defaultclient .execute(httpgetrequest); // grab response bufferedreader reader = new bufferedreader( new inputstreamreader(httpresponse.getentity() .getcontent(), "utf-8")); string result = reader.readline(); jsonobject object = new jsonobject(result); jsonarray jarray = object.getjsonarray("contact"); (int = 0; < jarray.length(); i++) { jsonobject jasonobject = jarray.getjsonobject(i); today_details = new todaydetails(); today_details.setid(jasonobject.getint("id")); today_details.setname(jasonobject.getstring("name")); today_details.setemail(jasonobject.getstring("email")); today_details.setaddress(jasonobject.getstring("address")); today_details.setgender(jasonobject.getstring("gender")); today_details.setphone(jasonobject.getint("phone")); today_details.sethome(jasonobject.getint("home")); today_details.setmobile(jasonobject.getint("mobile")); today_details.setoffice(jasonobject.getint("office")); search_results.add(today_details); } } catch(exception e){ e.printstacktrace(); } return search_results; } protected void onpostexecute(arraylist<todaydetails> search_results) { if(weathertoday.this.pd != null){ weathertoday.this.pd.dismiss(); } arraylist<todaydetails> today_details = search_results; list_view_main = (listview)findviewbyid(r.id.todaylist); list_view_main.setadapter(new todaylistadapter(getapplicationcontext(), today_details)); } }; }
sanitary check when working json
check error on json side. useful link json validation.
jsonlinttry link validate json,just copy/paste json , check if valid or not
if valid post on here , may able more.
if not valid...well fine :-)
code check
- add complete json reading line line stringbuilder
- call tostring , pass resulting json string jsonobject obj=new jsonobject(string) function.
- so if exception bad in json check via above mention link.
- if works fine , iterate jsonobject array , array of object or whatever.
i hope you'll find useful
update
using gson library quite useful json gets bigger.it handles every thing you.abstract in nature, , provides static method retrieve json or make one.
here's gist of programs made json parsing using gson library.
Comments
Post a Comment