java - Loading local JsonData while no Internet connection is present -
i making android app in want load json data. specification that, if there internet available, load data url provide.
if there no internet, load local json.txt file.
my app able load data url if there internet connection , can load local json data if there absolute no internet connection.
however in meta state such this, app crashes.
the log
07-14 09:56:21.258 22499-28349/scientist.jobless.foodmana e/log_tag﹕ error in http connection java.net.unknownhostexception: unable resolve host "lit-hamlet-6856.herokuapp.com": no address associated hostname 07-14 09:56:21.258 22499-28349/scientist.jobless.foodmana e/log_tag﹕ error converting result java.lang.nullpointerexception 07-14 09:56:21.258 22499-28349/scientist.jobless.foodmana e/log_tag﹕ error parsing data org.json.jsonexception: end of input @ character 0 of 07-14 09:56:21.266 22499-28349/scientist.jobless.foodmana e/androidruntime﹕ fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:299) @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) @ java.util.concurrent.futuretask.setexception(futuretask.java:124) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:856) caused by: java.lang.nullpointerexception @ scientist.jobless.foodmana.jsonloadsubfirstfirst$downloadjson.doinbackground(jsonloadsubfirstfirst.java:239) @ scientist.jobless.foodmana.jsonloadsubfirstfirst$downloadjson.doinbackground(jsonloadsubfirstfirst.java:215) @ android.os.asynctask$2.call(asynctask.java:287) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) at java.util.concurrent.futuretask.run(futuretask.java:137) at android.os.asynctask$serialexecutor$1.run(asynctask.java:230) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) at java.lang.thread.run(thread.java:856) 07-14 09:56:21.878 22499-22499/scientist.jobless.foodmana e/windowmanager﹕ activity scientist.jobless.foodmana.jsonloadsubfirstfirst has leaked window com.android.internal.policy.impl.phonewindow$decorview@5374c12c added here android.view.windowleaked: activity scientist.jobless.foodmana.jsonloadsubfirstfirst has leaked window com.android.internal.policy.impl.phonewindow$decorview@5374c12c added here @ android.view.viewrootimpl.<init>(viewrootimpl.java:374) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:292) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:224) @ android.view.windowmanagerimpl$compatmodewrapper.addview(windowmanagerimpl.java:149) @ android.view.window$localwindowmanager.addview(window.java:547) @ android.app.dialog.show(dialog.java:277) @ scientist.jobless.foodmana.mycustomprogressdialog.show(mycustomprogressdialog.java:43) @ scientist.jobless.foodmana.jsonloadsubfirstfirst$downloadjson.onpreexecute(jsonloadsubfirstfirst.java:227) @ android.os.asynctask.executeonexecutor(asynctask.java:586) @ android.os.asynctask.execute(asynctask.java:534) @ scientist.jobless.foodmana.jsonloadsubfirstfirst.oncreate(jsonloadsubfirstfirst.java:74) @ android.app.activity.performcreate(activity.java:5008) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1079) @ android.app.activitythread.performlaunchactivity(activitythread.java:2023) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2084) @ android.app.activitythread.access$600(activitythread.java:130) @ android.app.activitythread$h.handlemessage(activitythread.java:1195) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4745) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method)
below java code checking connection type.
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view listview_main.xml setcontentview(r.layout.listview_main); // execute downloadjson asynctask connectivitymanager connec = (connectivitymanager)getsystemservice(context.connectivity_service); if (connec != null && ( (connec.getnetworkinfo(connectivitymanager.type_wifi).getstate() == networkinfo.state.connected) || (connec.getnetworkinfo(connectivitymanager.type_mobile).getstate() == networkinfo.state.connected))) { new downloadjson(this).execute(); //you connected, online. } else if (connec != null && ( (connec.getnetworkinfo(connectivitymanager.type_wifi).getstate() == networkinfo.state.disconnected) || (connec.getnetworkinfo(connectivitymanager.type_mobile).getstate() == networkinfo.state.disconnected ))) { //not connected. toast.maketext(getapplicationcontext(), "you must connected internet", toast.length_long).show(); new localdownloadjson().execute(); } else if (connec != null && ( (connec.getnetworkinfo(connectivitymanager.type_wifi).getstate() == networkinfo.state.unknown) || (connec.getnetworkinfo(connectivitymanager.type_mobile).getstate() == networkinfo.state.unknown ))) { //i want load local json data here } }
my question how handle meta state of connection have shown in emulator's picture.
i have tried this answer error on isnetworkavailable(context)
cannot resolve method isnetworkavailable.
is there anyway code ??
(connec != null && ( (connec.getnetworkinfo(connectivitymanager.type_wifi).getstate() == networkinfo.state.unknown) || (connec.getnetworkinfo(connectivitymanager.type_mobile).getstate() == networkinfo.state.unknown )))
localjsondownload.java
private class localdownloadjson extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); pd = new progressdialog(jsonloadsubfirstfirst.this); pd.setmessage("loading..."); pd.setprogressstyle(progressdialog.style_spinner); pd.setindeterminate(true); pd.setcancelable(false); pd.show(); } @override protected void doinbackground(void... params) { // create array localarraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address stringbuffer sb = new stringbuffer(); bufferedreader br = null; try { br = new bufferedreader(new inputstreamreader(getassets().open("localfood.txt"))); string temp; while ((temp = br.readline()) != null) sb.append(temp); } catch (ioexception e) { e.printstacktrace(); } { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } myjsonstring = sb.tostring(); try{ jsonobject jsonobj = new jsonobject(myjsonstring); jsonarray attendance = jsonobj.getjsonarray("events"); (int = 0; < attendance.length(); i++) { jsonobject = attendance.getjsonobject(i); string name = a.getstring("name"); string time = a.getstring("time"); string ingredients = a.getstring("ingredients"); string serves = a.getstring("serves"); string descr = a.getstring("description"); hashmap<string, string> contact = new hashmap<string, string>(); // adding each child node hashmap key => value contact.put("name", name); contact.put("time", time); contact.put("ingredients", ingredients); contact.put("serves", serves); contact.put("description", descr); // adding contact contact list localarraylist.add(contact); } } catch (jsonexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void args) { toast.maketext(getapplicationcontext(),"loading local data dishes, please connect o internet load new dishes",toast.length_long).show(); // locate listview in listview_main.xml //setcontentview(r.layout.listview_main); listview = (listview) findviewbyid(r.id.listview); // pass results listviewadapter.java adapter = new listviewadapter(jsonloadsubfirstfirst.this, localarraylist); // set adapter listview listview.setadapter(adapter); // close progressdialog // mprogressdialog.dismiss(); // textview.setvisibility(view.visible); pd.dismiss(); super.onpostexecute(args); } }
you can 1 thing while loading json data when internet available can save sqite , while loading next time check if network available or not if network not available can fetch json data sqlite , parse simple. , can use greendao make fast , reliable
Comments
Post a Comment