android - Reading a json file from sdcard returns - null pointer exception -
i searched through related threads , not successful , related answers welcomed .
i have json info .json
file in sdcard within .newfolder
folder , want read info in file , parse , find timestamp , want rewrite if timestamp different .
the below code utilize , , info in abuffer
, value in jdata_fromfile
nullpointerexception while parsing .
myfile = new file(environment.getexternalstoragedirectory() + "/.newfolder/" +str.get(i)); if(myfile.exists()) { fileinputstream fin = new fileinputstream(myfile); bufferedreader myreader = new bufferedreader(new inputstreamreader(fin)); string adatarow = ""; string abuffer = ""; while ((adatarow = myreader.readline()) != null) { abuffer += adatarow; } myreader.close(); jdata_fromfile = new jsonobject(abuffer); string timestamp_fromfile = jdata_fromfile.getstring(str_timestamp.get(i)); timestamp_val_fromfile.add(timestamp_fromfile); } else { myfile.createnewfile(); writedate_tofile(value_json.tostring()); timestamp_val_fromfile.add("xxx"); }
and , when open file in notepad , in single line showing part of info , when re-create http://jsonformatter.curiousconcept.com
i whole info , valid json . see rest of info in notepad when - ctrl+end , press come in .
why , go wrong ?
this error - log
02-13 18:44:52.109: e/abuffer value(7134): {"status":{"itemlist":[{"x - drinks":{"details":[{"type":"","image":"","price":"","name":"minerals"},{"type":"","image":"","price":"","name":"milk shakes"},{"type":"","image":"","price":"","name":"milk"},{"type":"","image":"","price":"","name":"mineral water"},{"type":"","image":"","price":"","name":"hot beverages"},{"type":"","image":"","price":"","name":"chocolate muffin , water ice cream"}, ........................................................................{"type":"","image":"","price":"","name":"blu 02-13 18:44:52.189: e/jdata_fromfile value(7134): {"x time_stamp":"2013-02-12 12:30:00","status":{"itemlist":[{"x - sides":{"details":[{"type":"","image":"","name":"onion rings (6)","price":""},{"type":"","image":"","name":"sausage portion (minimum 2 per portion)","price":""},.................................................................................................................... ,"x - burgers":{"details":[{"type":"","image":"","name":"x 5oz burger","price":""},{"type":"","image":"","name":"x 5oz cheese burger","price":""},{"type":"","image":"","name":"x 5oz bacon , cheese","price":""},{"type":"","imag 02-13 18:44:52.189: e/reading internal file - exception e(7134): java.lang.nullpointerexception
thanks in advance .
i don't know what's wrong above code here solution using different json library.
i using next code in 1 of android application , works reading json file android file system. instead of using android's json library, used json-simple.jar
http://code.google.com/p/json-simple/
put above jar in libs folder of android project. , set next permission in .manifest
file.
<uses-permission android:name="android.permission.read_external_storage"/>
then set follwing code in separate thread.
jsonparser parser = new jsonparser(); seek { string path = environment.getexternalstoragedirectory() + "/download/list.json"; object object = parser.parse(new filereader(path)); jsonobject jsonobject = (jsonobject) object; //use jsonarray instead of jsonobject if json file contains array of jsons // // jsonobject // } catch(exception e) { // todo: handle exception }
here list.json
file @ location /mnt/sdcard/download
.
for reference here illustration using above library in simple java project.
android json android-sdcard read-data
No comments:
Post a Comment