从sdcard读取json文件返回 - 空指针异常

时间:2013-02-13 12:57:10

标签: android json android-sdcard

我搜索了所有相关主题并且我没有成功,欢迎任何相关的答案。

我在.json文件夹中的SDCard中有一个JSON数据作为.NewFolder文件, 我想读取文件中的数据并解析,找到时间戳,并希望在时间戳不同时重写它。

以下是我使用的代码,我获取aBuffer中的数据和jdata_fromfile中的值,但是在解析时我得到nullPointerException。

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");
}

而且,当我在记事本中打开文件时,它只是一行显示数据的一部分,当我复制到http://jsonformatter.curiousconcept.com

我得到了整个数据和一个有效的json。 我可以在记事本中看到其余的数据 - 按ctrl +结束并按回车键。

为什么会这样,我哪里出错?

这是我的错误 - 记录

02-13 18:44:52.109: E/aBuffer value(7134):  is {"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 and Ice Cream"}, ........................................................................{"type":"","image":"","price":"","name":"Blu
02-13 18:44:52.189: E/jdata_fromfile value(7134):  is {"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 with Bacon and Cheese","price":""},{"type":"","imag
02-13 18:44:52.189: E/Reading from internal file - Exception e(7134): java.lang.NullPointerException

提前致谢。

1 个答案:

答案 0 :(得分:1)

我不知道上面的代码有什么问题,但这里是使用不同JSON库的解决方案。

我在我的一个Android应用程序中使用以下代码,它可以完美地从android文件系统中读取任何json文件。我使用了http://code.google.com/p/json-simple/

中的json-simple.jar,而不是使用Android的JSON库

将上面的jar放在Android项目的libs文件夹中。并在.Manifest文件中设置以下权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

然后将以下代码放在单独的Thread中。

JSONParser parser  = new JSONParser();
try { 
    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
    //
    // Do anything with jsonObject  
    //
} catch(Exception e) {
    // TODO: handle exception
}

我的list.json文件位于/mnt/sdcard/Download位置。

供参考here是在简单的java项目中使用上述库的示例。