How to parse or read json file from external storage on Android app -
i implemented parsing json server (url). couldn't find way parse json sdcard (/download/example.json). can me solve issue/change code?
i used asynctask this. sample tutorial or sample code more appreciated. (sorry english.)
public class main extends activity { private textview shopsdisplay; private static string searchurl = "http://example.com/sample.json"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.baby); //reference throughout class shopsdisplay = (textview)findviewbyid(r.id.tweet_txt); new getshops().execute(searchurl); } private class getshops extends asynctask<string, void, string> { /* * carry out fetching task in background * - receives search url via execute method */ @override protected string doinbackground(string... shopsurl) { //start building result json string stringbuilder shopsfeedbuilder = new stringbuilder(); //should 1 url, receives array (string searchurl : shopsurl) { httpclient shopsclient = new defaulthttpclient(); try { //pass search url string fetch httpget shopsget = new httpget(searchurl); //execute request httpresponse shopsresponse = shopsclient.execute(shopsget); //check status, proceed if ok statusline searchstatus = shopsresponse.getstatusline(); if (searchstatus.getstatuscode() == 200) { //get response httpentity shopsentity = shopsresponse.getentity(); inputstream shopscontent = shopsentity.getcontent(); //process results inputstreamreader shopsinput = new inputstreamreader(shopscontent); bufferedreader shopsreader = new bufferedreader(shopsinput); string linein; while ((linein = shopsreader.readline()) != null) { shopsfeedbuilder.append(linein); } } else shopsdisplay.settext("whoops - went wrong!"); } catch(exception e){ shopsdisplay.settext("whoops - went wrong!"); e.printstacktrace(); } } //return result string return shopsfeedbuilder.tostring(); } /* * process result of search query * - receives json string representing shops search term included */ protected void onpostexecute(string result) { //start preparing result string display stringbuilder shopsresultbuilder = new stringbuilder(); try { //get jsonobject result jsonobject resultobject = new jsonobject(result); //get jsonarray contained within jsonobject retrieved - "results" jsonarray shopsarray = resultobject.getjsonarray("shops"); //loop through each item in shops array (int t=0; t<shopsarray.length(); t++) { //each item jsonobject jsonobject shopsobject = shopsarray.getjsonobject(t); //for if condition string id = (string) shopsobject.get("id"); //get name , description each shops if (id.equals("550")){ shopsresultbuilder.append(shopsobject.getstring("name")+": "); shopsresultbuilder.append(shopsobject.get("description")+"\n\n"); } } } catch (exception e) { shopsdisplay.settext("whoops - went wrong!"); e.printstacktrace(); } //check result exists if(shopsresultbuilder.length()>0) shopsdisplay.settext(shopsresultbuilder.tostring()); else shopsdisplay.settext("sorry - no shops found search!"); } } }
for jsonobject
, use standard java file i/o read file string
, pass jsonobject
constructor.
if have large json file, though, may wish switch other json parser (e.g., jsonreader
), may give different options (e.g., use filereader
jsonreader
).
Comments
Post a Comment