Listview in Android while getting data from a server -
i trying populate data listview asynchronously
i retrieving data server json response
mainactivity.java
public class mainactivity extends activity { // url make request private static string url="http://54.218.73.244:7002/"; listview yourlistview; list<item> yourdata = new arraylist<item>(); progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); yourlistview = (listview) findviewbyid(r.id.listviewid); //instantiating progressdialog oncreate method progressdialog=new progressdialog(mainactivity.this); new parsingasync().execute(); } private class parsingasync extends asynctask<void, void, void> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); progressdialog=progressdialog.show(mainactivity.this, "", "please wait", true, false); } @override protected void doinbackground(void... params) { // todo auto-generated method stub //create json parser instance ----- used json parser android jsonobjparser jparser=new jsonobjparser(); //getting json string url ------ used json array android jsonarray json=jparser.getjsonfromurl(url); list<item> yourdata = new arraylist<item>(); try { for(int i=0;i<json.length();i++) { jsonobject c=json.getjsonobject(i);// used json object android string restaurant_name=c.getstring("restaurantname"); yourdata.add(new item(restaurant_name)); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); progressdialog.dismiss(); listadapter customadapter = new listadapter(mainactivity.this, r.layout.itemlistrow, yourdata); yourlistview.setadapter(customadapter); } } }
jsonobjparser.java
public class jsonobjparser { static inputstream = null; static jsonarray jobj = null; static string json = ""; // constructor public jsonobjparser() { } public jsonarray getjsonfromurl(string url) { // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); 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 jsonarray(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
log::
08-23 20:43:59.442: w/dalvikvm(1311): threadid=6: spin on suspend #1 threadid=9 (pcf=0) 08-23 20:43:59.442: d/dalvikvm(1311): temporarily moving tid 1320 fg (was 0) 08-23 20:43:59.442: d/dalvikvm(1311): temporarily raised priority on tid 1320 (10 -> 0) 08-23 20:43:59.528: w/dalvikvm(1311): threadid=6: spin on suspend resolved in 1085 msec 08-23 20:43:59.528: d/dalvikvm(1311): restored policy of 1320 0 08-23 20:43:59.528: d/dalvikvm(1311): restored priority on 1320 10 08-23 20:44:02.460: e/json parser(1311): error parsing data org.json.jsonexception: value {"restaurants":[{"restaurantid":1,"restaurantname":"copperchimney"},{"restaurantid":2,"restaurantname":"aroy"},{"restaurantid":3,"restaurantname":"markboulevard"},{"restaurantid":4,"restaurantname":"indian"}],"restauranttimings":[{"_id":1,"restauranttime":"8pm 11pm"},{"_id":2,"restauranttime":"10pm 12pm"},{"_id":3,"restauranttime":"11pm 9pm"},{"_id":4,"restauranttime":"10pm 5pm"}]} of type org.json.jsonobject cannot converted jsonarray 08-23 20:44:02.470: w/dalvikvm(1311): threadid=9: thread exiting uncaught exception (group=0x40015560) 08-23 20:44:02.489: e/androidruntime(1311): fatal exception: asynctask #1 08-23 20:44:02.489: e/androidruntime(1311): java.lang.runtimeexception: error occured while executing doinbackground() 08-23 20:44:02.489: e/androidruntime(1311): @ android.os.asynctask$3.done(asynctask.java:200) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.futuretask.setexception(futuretask.java:125) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.futuretask.run(futuretask.java:138) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 08-23 20:44:02.489: e/androidruntime(1311): @ java.lang.thread.run(thread.java:1019) 08-23 20:44:02.489: e/androidruntime(1311): caused by: java.lang.nullpointerexception 08-23 20:44:02.489: e/androidruntime(1311): @ com.project.findmybuffet.mainactivity$parsingasync.doinbackground(mainactivity.java:78) 08-23 20:44:02.489: e/androidruntime(1311): @ com.project.findmybuffet.mainactivity$parsingasync.doinbackground(mainactivity.java:1) 08-23 20:44:02.489: e/androidruntime(1311): @ android.os.asynctask$2.call(asynctask.java:185) 08-23 20:44:02.489: e/androidruntime(1311): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 08-23 20:44:02.489: e/androidruntime(1311): ... 4 more 08-23 20:44:04.531: e/windowmanager(1311): activity com.project.findmybuffet.mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4051f4e8 added here 08-23 20:44:04.531: e/windowmanager(1311): android.view.windowleaked: activity com.project.findmybuffet.mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4051f4e8 added here 08-23 20:44:04.531: e/windowmanager(1311): @ android.view.viewroot.<init>(viewroot.java:258) 08-23 20:44:04.531: e/windowmanager(1311): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:148) 08-23 20:44:04.531: e/windowmanager(1311): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:91) 08-23 20:44:04.531: e/windowmanager(1311): @ android.view.window$localwindowmanager.addview(window.java:424) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.dialog.show(dialog.java:241) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.progressdialog.show(progressdialog.java:107) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.progressdialog.show(progressdialog.java:95) 08-23 20:44:04.531: e/windowmanager(1311): @ com.project.findmybuffet.mainactivity$parsingasync.onpreexecute(mainactivity.java:58) 08-23 20:44:04.531: e/windowmanager(1311): @ android.os.asynctask.execute(asynctask.java:391) 08-23 20:44:04.531: e/windowmanager(1311): @ com.project.findmybuffet.mainactivity.oncreate(mainactivity.java:47) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.activitythread.access$1500(activitythread.java:117) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 08-23 20:44:04.531: e/windowmanager(1311): @ android.os.handler.dispatchmessage(handler.java:99) 08-23 20:44:04.531: e/windowmanager(1311): @ android.os.looper.loop(looper.java:123) 08-23 20:44:04.531: e/windowmanager(1311): @ android.app.activitythread.main(activitythread.java:3683) 08-23 20:44:04.531: e/windowmanager(1311): @ java.lang.reflect.method.invokenative(native method) 08-23 20:44:04.531: e/windowmanager(1311): @ java.lang.reflect.method.invoke(method.java:507) 08-23 20:44:04.531: e/windowmanager(1311): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 08-23 20:44:04.531: e/windowmanager(1311): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 08-23 20:44:04.531: e/windowmanager(1311): @ dalvik.system.nativestart.main(native method) 08-23 20:49:02.664: i/process(1311): sending signal. pid: 1311 sig: 9 08-23 21:12:52.021: e/json parser(1350): error parsing data org.json.jsonexception: value {"restaurants":[{"restaurantid":1,"restaurantname":"copperchimney"},{"restaurantid":2,"restaurantname":"aroy"},{"restaurantid":3,"restaurantname":"markboulevard"},{"restaurantid":4,"restaurantname":"indian"}],"restauranttimings":[{"_id":1,"restauranttime":"8pm 11pm"},{"_id":2,"restauranttime":"10pm 12pm"},{"_id":3,"restauranttime":"11pm 9pm"},{"_id":4,"restauranttime":"10pm 5pm"}]} of type org.json.jsonobject cannot converted jsonarray 08-23 21:12:52.030: w/dalvikvm(1350): threadid=9: thread exiting uncaught exception (group=0x40015560) 08-23 21:12:52.040: e/androidruntime(1350): fatal exception: asynctask #1 08-23 21:12:52.040: e/androidruntime(1350): java.lang.runtimeexception: error occured while executing doinbackground() 08-23 21:12:52.040: e/androidruntime(1350): @ android.os.asynctask$3.done(asynctask.java:200) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.futuretask.setexception(futuretask.java:125) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.futuretask.run(futuretask.java:138) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 08-23 21:12:52.040: e/androidruntime(1350): @ java.lang.thread.run(thread.java:1019) 08-23 21:12:52.040: e/androidruntime(1350): caused by: java.lang.nullpointerexception 08-23 21:12:52.040: e/androidruntime(1350): @ com.project.findmybuffet.mainactivity$parsingasync.doinbackground(mainactivity.java:78) 08-23 21:12:52.040: e/androidruntime(1350): @ com.project.findmybuffet.mainactivity$parsingasync.doinbackground(mainactivity.java:1) 08-23 21:12:52.040: e/androidruntime(1350): @ android.os.asynctask$2.call(asynctask.java:185) 08-23 21:12:52.040: e/androidruntime(1350): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 08-23 21:12:52.040: e/androidruntime(1350): ... 4 more 08-23 21:12:54.300: e/windowmanager(1350): activity com.project.findmybuffet.mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4051f950 added here 08-23 21:12:54.300: e/windowmanager(1350): android.view.windowleaked: activity com.project.findmybuffet.mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@4051f950 added here 08-23 21:12:54.300: e/windowmanager(1350): @ android.view.viewroot.<init>(viewroot.java:258) 08-23 21:12:54.300: e/windowmanager(1350): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:148) 08-23 21:12:54.300: e/windowmanager(1350): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:91) 08-23 21:12:54.300: e/windowmanager(1350): @ android.view.window$localwindowmanager.addview(window.java:424) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.dialog.show(dialog.java:241) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.progressdialog.show(progressdialog.java:107) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.progressdialog.show(progressdialog.java:95) 08-23 21:12:54.300: e/windowmanager(1350): @ com.project.findmybuffet.mainactivity$parsingasync.onpreexecute(mainactivity.java:58) 08-23 21:12:54.300: e/windowmanager(1350): @ android.os.asynctask.execute(asynctask.java:391) 08-23 21:12:54.300: e/windowmanager(1350): @ com.project.findmybuffet.mainactivity.oncreate(mainactivity.java:47) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.activitythread.access$1500(activitythread.java:117) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 08-23 21:12:54.300: e/windowmanager(1350): @ android.os.handler.dispatchmessage(handler.java:99) 08-23 21:12:54.300: e/windowmanager(1350): @ android.os.looper.loop(looper.java:123) 08-23 21:12:54.300: e/windowmanager(1350): @ android.app.activitythread.main(activitythread.java:3683) 08-23 21:12:54.300: e/windowmanager(1350): @ java.lang.reflect.method.invokenative(native method) 08-23 21:12:54.300: e/windowmanager(1350): @ java.lang.reflect.method.invoke(method.java:507) 08-23 21:12:54.300: e/windowmanager(1350): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 08-23 21:12:54.300: e/windowmanager(1350): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 08-23 21:12:54.300: e/windowmanager(1350): @ dalvik.system.nativestart.main(native method) 08-23 21:13:14.790: d/dalvikvm(1350): gc_external_alloc freed 282k, 52% free 2779k/5703k, external 2061k/2137k, paused 672ms 08-23 21:13:18.330: e/json parser(1350): error parsing data org.json.jsonexception: value {"restaurants":[{"restaurantid":1,"restaurantname":"copperchimney"},{"restaurantid":2,"restaurantname":"aroy"},{"restaurantid":3,"restaurantname":"markboulevard"},{"restaurantid":4,"restaurantname":"indian"}],"restauranttimings":[{"_id":1,"restauranttime":"8pm 11pm"},{"_id":2,"restauranttime":"10pm 12pm"},{"_id":3,"restauranttime":"11pm 9pm"},{"_id":4,"restauranttime":"10pm 5pm"}]} of type org.json.jsonobject cannot converted jsonarray 08-23 21:13:18.330: w/dalvikvm(1350): threadid=11: thread exiting uncaught exception (group=0x40015560)
json :: http://54.218.73.244:7002/
{ "restaurants": [ { "restaurantid": 1, "restaurantname": "copperchimney" }, { "restaurantid": 2, "restaurantname": "aroy" }, { "restaurantid": 3, "restaurantname": "markboulevard" }, { "restaurantid": 4, "restaurantname": "indian" } ], "restauranttimings": [ { "_id": 1, "restauranttime": "8pm 11pm" }, { "_id": 2, "restauranttime": "10pm 12pm" }, { "_id": 3, "restauranttime": "11pm 9pm" }, { "_id": 4, "restauranttime": "10pm 5pm" } ] }
try this
httpentity httpentity = httpresponse.getentity(); string _response=entityutils.tostring(httpentity); log.i("response",_response); jsonobject jsonobject = new jsonobject(_response);
to parse have shown how parse restaurants
array. similar parse other jsonarray restauranttimings
.
"restaurants": [ // array of restaurants { // json object "restaurantid": 1, // json string "restaurantname": "copperchimney" }, { "restaurantid": 2, "restaurantname": "aroy" }, { "restaurantid": 3, "restaurantname": "markboulevard" }, { "restaurantid": 4, "restaurantname": "indian" } ], try { jsonobject jsonobject = new jsonobject(_response); jsonarray jarr = jsonobject.getjsonarray("restaurants"); // json array restaurants for(int i=0;i<jarr.length();i++) { jsonobject jb = jarr.getjsonobject(i); string id = jb.getstring("restaurantid"); string name = jb.getstring("restaurantname"); toast.maketext(getapplicationcontext(),name, 1000).show(); } } catch(exception e) { }
also see this
log.i("parsing..", ""+jsonobject.get("student").tostring()); jobj = new jsonarray(jsonobject.get("student").tostring());
where student in json posted. looks doing wrong
edit :
public class mainactivity extends activity { progressdialog pd; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pd = new progressdialog(this); new thetask().execute(); } class thetask extends asynctask<void,string,string> { @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); pd.dismiss(); if(result!=null) { log.i("reponse",result); toast.maketext(getapplicationcontext(), result, 1000).show(); // } } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); pd.show(); } @override protected string doinbackground(void... params) { // todo auto-generated method stub string s =getdata(); return s; } private string getdata() { string _response = null; try { httpclient httpclient = new defaulthttpclient(); httpclient.getparams().setparameter(coreprotocolpnames.protocol_version, httpversion.http_1_1); httpget request = new httpget("http://54.218.73.244:7002/"); httpresponse response = httpclient.execute(request); httpentity resentity = response.getentity(); _response =entityutils.tostring(resentity); // jsonobject jsonobject = new jsonobject(_response); jsonarray jarr = jsonobject.getjsonarray("restaurants"); for(int i=0;i<jarr.length();i++) { jsonobject jb = jarr.getjsonobject(i); string id = jb.getstring("restaurantid"); string name = jb.getstring("restaurantname"); toast.maketext(getapplicationcontext(),name, 1000).show(); } } catch(exception e) { e.printstacktrace(); } return _response; } } }
response
08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "restaurants": [ 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "restaurantid": 1, 08-23 21:44:51.455: i/reponse(24732): "restaurantname": "copperchimney" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "restaurantid": 2, 08-23 21:44:51.455: i/reponse(24732): "restaurantname": "aroy" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "restaurantid": 3, 08-23 21:44:51.455: i/reponse(24732): "restaurantname": "markboulevard" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "restaurantid": 4, 08-23 21:44:51.455: i/reponse(24732): "restaurantname": "indian" 08-23 21:44:51.455: i/reponse(24732): } 08-23 21:44:51.455: i/reponse(24732): ], 08-23 21:44:51.455: i/reponse(24732): "restauranttimings": [ 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "_id": 1, 08-23 21:44:51.455: i/reponse(24732): "restauranttime": "8pm 11pm" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "_id": 2, 08-23 21:44:51.455: i/reponse(24732): "restauranttime": "10pm 12pm" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "_id": 3, 08-23 21:44:51.455: i/reponse(24732): "restauranttime": "11pm 9pm" 08-23 21:44:51.455: i/reponse(24732): }, 08-23 21:44:51.455: i/reponse(24732): { 08-23 21:44:51.455: i/reponse(24732): "_id": 4, 08-23 21:44:51.455: i/reponse(24732): "restauranttime": "10pm 5pm" 08-23 21:44:51.455: i/reponse(24732): } 08-23 21:44:51.455: i/reponse(24732): ] 08-23 21:44:51.455: i/reponse(24732): }
Comments
Post a Comment