How to get integer from json object async in android? -


original question -

i following tutorial on json in android, having problem json value using async in android. first created jsonparser class , added following -

    public jsonobject getjsonfromurl(string url) {      // making http request     try {         // defaulthttpclient         defaulthttpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url);          httpresponse httpresponse = httpclient.execute(httppost);         httpentity httpentity = httpresponse.getentity();         = httpentity.getcontent();                 } catch (unsupportedencodingexception e) {         e.printstacktrace();     } catch (clientprotocolexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }      try {         bufferedreader reader = new bufferedreader(new inputstreamreader(                 is, "iso-8859-1"), 8);         stringbuilder sb = new stringbuilder();         string line = null;         while ((line = reader.readline()) != null) {             sb.append(line + "\n");         }         is.close();         json = sb.tostring();     } catch (exception e) { //      log.e("buffer error", "error converting result " + e.tostring());     }      // try parse string json object     try {         jobj = new jsonobject(json);     } catch (jsonexception e) { //      log.e("json parser", "error parsing data " + e.tostring());     }      // return json string     return jobj; } 

then use in fragment async tried -

    class gettopicid extends asynctask<string, string, jsonobject> {     private progressdialog pdialog;     @override       protected void onpreexecute() {           super.onpreexecute();           pdialog = new progressdialog(getactivity());           pdialog.setmessage("getting topic of day ...");           pdialog.setindeterminate(false);           pdialog.setcancelable(true);           pdialog.show();     }     @override       protected jsonobject doinbackground(string... args) {       jsonparser jparser = new jsonparser();       // getting json url       jsonobject json = jparser.getjsonfromurl("http://medicalguru.in/android/tod.php");       return json;     }      @override        protected void onpostexecute(jsonobject json) {        pdialog.dismiss();        try {            topic_id = json.getint("value");           }         catch (jsonexception e) {         e.printstacktrace();       }        {            toast.maketext(getactivity(), "topic id -"+topic_id, toast.length_long).show();        }      } } 

but app crashing when execute async. logcat shows -

03-06 15:07:03.123: e/androidruntime(2041): fatal exception: main 03-06 15:07:03.123: e/androidruntime(2041): java.lang.nullpointerexception 03-06 15:07:03.123: e/androidruntime(2041):     @ in.medicalguru.dailytestfragment$gettopicid.onpostexecute(dailytestfragment.java:786) 03-06 15:07:03.123: e/androidruntime(2041):     @ in.medicalguru.dailytestfragment$gettopicid.onpostexecute(dailytestfragment.java:1) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.os.asynctask.finish(asynctask.java:631) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.os.asynctask.access$600(asynctask.java:177) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.os.handler.dispatchmessage(handler.java:99) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.os.looper.loop(looper.java:137) 03-06 15:07:03.123: e/androidruntime(2041):     @ android.app.activitythread.main(activitythread.java:5103) 03-06 15:07:03.123: e/androidruntime(2041):     @ java.lang.reflect.method.invokenative(native method) 03-06 15:07:03.123: e/androidruntime(2041):     @ java.lang.reflect.method.invoke(method.java:525) 03-06 15:07:03.123: e/androidruntime(2041):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 03-06 15:07:03.123: e/androidruntime(2041):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-06 15:07:03.123: e/androidruntime(2041):     @ dalvik.system.nativestart.main(native method) 

the link mentioned above in code outputs -

{"value":1} 

when opened normally, when run in async says err related nullpointer? couldn't figure, making mistake ?


edit 1 : *tod.php* code (since matthew has pointed dont understand, may helpful)

<?php $today = date("ymd"); switch($today){ case 20140304 : $tod["value"] = 24; break; case 20140305 : $tod["value"] = 25; break; case 20140306 : $tod["value"] = 1; break; default: $tod["value"] = 1; break; } echo json_encode($tod); ?> 

edit 2 : *found problem (fully me) partly in real* - special matthew idea, uncomment error logs of function getjsonfromurl(string url). got new error line in logcat -

error parsing data org.json.jsonexception: value  of type java.lang.string cannot converted jsonobject 

after little bit of search on google , stacksoverflow found 1 suggestion change chrset utf-8 instead of iso-8859-1. , worked requested url in both situations (with or without www - suggestion test, nikis). answer partial real, because, 2 unanswered questions developed - 1. why did chrset change worked? in future, chrset used, avoid problem, , what's use of other 1 chrset? 2. matthew has replicated json server mwesly.com/test/ . trial on server, logcat shows following error in cases (with or without www, or without using index.php in end) -

error parsing data org.json.jsonexception: value <!doctype of type java.lang.string cannot converted jsonobject 

now, why error appearing on server? should done prevent/treat error. if need ask these updated questions separately, please let me know.

it looks json null on following line, giving nullpointerexception.

topic_id = json.getint("value"); 

i think because parser having issues parse json. wrote quick python script , tried parse page , failed well. reason, response http://medicalguru.in/android/tod.php is:

\xef\xbb\xbf{"value":1} 

i not sure why \xef\xbb\xbf bytes there, causing problem.

edit: quick google search says utf-8 bom. if remove android code should work.

edit2: put example json on http://mwesly.com/test. try hitting json see if android code correct. if works, means there problem endpoint, not android code.

edit3: example of using loopj:

asynchttpclient client = new asynchttpclient(); client.get("http://mwesly.com/test", null, new jsonhttpresponsehandler() {     @override     public void onsuccess(jsonobject json) {         // json         int value = json.getint("value");     } }); 

i'm going head bed, if still haven't solved problem when wake up, i'll give look. luck , happy coding!


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -