java - Troubleshooting JSONException: End of input at character 0 -
i doing search on active.com using api , search getting stuck @ error : org.json.jsonexception: end of input @ character 0. newbie,any appreciated.
mainactivity
private class searchtask extends asynctask<string, integer, string> { progressdialog dialog; @override protected void onpreexecute() { dialog = progressdialog.show(stayactiveactivity.this,"","please wait..."); super.onpreexecute(); } @override protected string doinbackground(string... params) { try { string result = activehelper.download(params [0]); return result; } catch (apiexception e) { e.printstacktrace(); log.e("alatta", "problem making search request"); } return ""; } @override protected void onpostexecute(string result) { try { jsonobject obj = new jsonobject(result); m_results = obj.getjsonarray("results"); if (m_results == null || m_results.length() == 0) { toast.maketext(getapplicationcontext(), "no results found " + m_search_text.gettext(), toast.length_long).show(); } else m_search_results.setadapter(new jsonadapter(getapplicationcontext())); } catch (jsonexception e) { e.printstacktrace(); } } } private class jsonadapter extends baseadapter { public jsonadapter(context c){ } public int getcount() { return m_results.length(); } public object getitem(int arg0){ return null; } public long getitemid(int pos){ return pos; } public view getview(int pos, view convertview, viewgroup parent) { view tv; textview t; if (convertview == null) tv = m_inflater.inflate (r.layout.item, parent, false); else tv = convertview; try { /* each entry in listview, need populate * text , timestamp */ t = (textview) tv.findviewbyid(r.id.text); jsonobject obj = m_results.getjsonobject(pos); t.settext (obj.getstring("event") + ": " + obj.getstring("text")); t = (textview) tv.findviewbyid(r.id.created_at); t.settext (obj.getstring("created_at")); } catch (jsonexception e) { log.e("alatta", e.getmessage()); } return tv; } } activehelper
public class activehelper { private static final string active_search = "http://api.amp.active.com/search?&v=json&api_key=rm4agnjv6k4m4tvpft95xvbn"; private static final int http_status_ok = 200; private static byte[] buff = new byte[1024]; public static class searchexception extends exception { public searchexception (string msg) { super (msg); } public searchexception (string msg, throwable thr) { super (msg, thr); } } protected static synchronized string download (string parm) throws searchexception { string url = active_search+ parm; httpclient client = new defaulthttpclient(); httpget request = new httpget(url); try { httpresponse response = client.execute(request); statusline status = response.getstatusline(); if (status.getstatuscode() != http_status_ok) throw new searchexception("invalid response search.active.com" + status.tostring()); httpentity entity = response.getentity(); inputstream ist = entity.getcontent(); bytearrayoutputstream content = new bytearrayoutputstream(); int readcount = 0; while ((readcount = ist.read(buff)) != -1) content.write(buff, 0, readcount); return new string (content.tobytearray()); } catch (exception e) { e.printstacktrace(); throw new searchexception("problem using api", e); } } } stacktrace
06-11 15:01:27.569: e/alatta(7698): problem making search request 06-11 15:01:27.569: w/system.err(7698): org.json.jsonexception: end of input @ character 0 of 06-11 15:01:27.569: w/system.err(7698): @ org.json.jsontokener.syntaxerror(jsontokener.java:446) 06-11 15:01:27.569: w/system.err(7698): @ org.json.jsontokener.nextvalue(jsontokener.java:93) 06-11 15:01:27.569: w/system.err(7698): @ org.json.jsonobject.<init>(jsonobject.java:154) 06-11 15:01:27.569: w/system.err(7698): @ org.json.jsonobject.<init>(jsonobject.java:171) 06-11 15:01:27.569: w/system.err(7698): @ my.stayactive.plan.stayactiveactivity$searchtask.onpostexecute(stayactiveactivity.java:120) 06-11 15:01:27.569: w/system.err(7698): @ my.stayactive.plan.stayactiveactivity$searchtask.onpostexecute(stayactiveactivity.java:1) 06-11 15:01:27.569: w/system.err(7698): @ android.os.asynctask.finish(asynctask.java:417) 06-11 15:01:27.569: w/system.err(7698): @ android.os.asynctask.access$300(asynctask.java:127) 06-11 15:01:27.579: w/system.err(7698): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:429) 06-11 15:01:27.579: w/system.err(7698): @ android.os.handler.dispatchmessage(handler.java:99) 06-11 15:01:27.579: w/system.err(7698): @ android.os.looper.loop(looper.java:150) 06-11 15:01:27.579: w/system.err(7698): @ android.app.activitythread.main(activitythread.java:4293) 06-11 15:01:27.579: w/system.err(7698): @ java.lang.reflect.method.invokenative(native method) 06-11 15:01:27.579: w/system.err(7698): @ java.lang.reflect.method.invoke(method.java:507) 06-11 15:01:27.579: w/system.err(7698): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:849) 06-11 15:01:27.579: w/system.err(7698): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:607) 06-11 15:01:27.579: w/system.err(7698): @ dalvik.system.nativestart.main(native method)
in onpostexecute(...) think need use _results name of jsonarray , not results...
m_results = obj.getjsonarray("results"); // change "_results"
Comments
Post a Comment