Thursday, 15 July 2010

android - Same code gives different result in different Activities while pulling JSON data through AsyncTask -



android - Same code gives different result in different Activities while pulling JSON data through AsyncTask -

this simple code snippet pull json info localhost

public class jsontest extends actionbaractivity { private progressdialog dialog; string message3; string message_short; string[] items; string[] short_items; textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_jsontest); string localhost = "http://10.0.2.2/example/json/notification.php"; tv = (textview) findviewbyid(r.id.textview1); new readjsonfeedtask().execute(localhost); } private class readjsonfeedtask extends asynctask<string, void, string> { protected void onpreexecute() { super.onpreexecute(); dialog = new progressdialog(jsontest.this); dialog.setmessage("downloading notifications. please wait . . ."); dialog.setindeterminate(false); dialog.setcancelable(false); dialog.show(); } protected string doinbackground(string...urls) { homecoming readjsonfeed(urls[0]); } protected void onpostexecute(string result) { dialog.dismiss(); seek { jsonarray jsonarray = new jsonarray(result); items = new string[jsonarray.length()]; short_items = new string[jsonarray.length()]; for(int = 0; < jsonarray.length(); i++) { jsonobject jobj = jsonarray.getjsonobject(i); message3 = "part 1 : " + jobj.getstring("subject") + "\n\n" + "part 2 : " + jobj.getstring("date"); items[i] = message3; message_short = message_short + "\n\n" + message3; } } catch(exception e) { e.printstacktrace(); } tv.settext(message_short); } } public string readjsonfeed(string url) { stringbuilder sb = new stringbuilder(); httpclient client = new defaulthttpclient(); httpget hg = new httpget(url); seek { httpresponse response = client.execute(hg); statusline statusline = response.getstatusline(); int statuscode = statusline.getstatuscode(); if(statuscode == 200) { httpentity en = response.getentity(); inputstream content = en.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(content)); string line; while((line = reader.readline()) != null) { sb.append(line); } } else { log.e("json", "failed download file"); } } catch(clientprotocolexception e) { e.printstacktrace(); } catch(ioexception e) { e.printstacktrace(); } homecoming sb.tostring(); } }

i used exact same code in 2 different activities in same app. difference name of classes. while 1 activity works perfect, activity gives error nil happens , textview left black. log logcat activity not work :

11-04 11:14:40.679: w/egl_emulation(2925): eglsurfaceattrib not implemented 11-04 11:14:40.749: w/system.err(2925): java.lang.nullpointerexception 11-04 11:14:40.749: w/system.err(2925): @ com.onclaveinc.ioexception.homescreen$readjsonfeedtask.onpostexecute(homescreen.java:129) 11-04 11:14:40.749: w/system.err(2925): @ com.onclaveinc.ioexception.homescreen$readjsonfeedtask.onpostexecute(homescreen.java:1) 11-04 11:14:40.749: w/system.err(2925): @ android.os.asynctask.finish(asynctask.java:632) 11-04 11:14:40.749: w/system.err(2925): @ android.os.asynctask.access$600(asynctask.java:177) 11-04 11:14:40.749: w/system.err(2925): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) 11-04 11:14:40.749: w/system.err(2925): @ android.os.handler.dispatchmessage(handler.java:102) 11-04 11:14:40.749: w/system.err(2925): @ android.os.looper.loop(looper.java:136) 11-04 11:14:40.749: w/system.err(2925): @ android.app.activitythread.main(activitythread.java:5017) 11-04 11:14:40.749: w/system.err(2925): @ java.lang.reflect.method.invokenative(native method) 11-04 11:14:40.749: w/system.err(2925): @ java.lang.reflect.method.invoke(method.java:515) 11-04 11:14:40.749: w/system.err(2925): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 11-04 11:14:40.749: w/system.err(2925): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 11-04 11:14:40.769: w/system.err(2925): @ dalvik.system.nativestart.main(native method)

can please help me this? need both of them work.

android json android-asynctask

No comments:

Post a Comment