1

我正在尝试将字符串中的 Json 响应作为缓存存储到文本文件中。但是当我从文本文件中读取它时,我无法解析它。

    try {
        BufferedReader reader = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory()
                 +File.separator
                 +"myDirectory" //folder name
                 +File.separator
                 +"roster.txt"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            Log.i("School FTB Cache", line);
            line = line + "\n";
        }

    JSONArray rosterJsonArray = new JSONArray(line);
    Log.i("School FTB Cache", rosterJsonArray.toString());
    JSONObject jObject = null;
    Roster roster = new Roster();
    rosterList = new ArrayList<Roster>(rosterJsonArray.length());
    int i=0;

        while(i<rosterJsonArray.length()){
            roster = new Roster();
            jObject = rosterJsonArray.getJSONObject(i);
            Log.e("roster", jObject.toString());
            roster.setId(Integer.valueOf(jObject.getString("id")));
            roster.setName(jObject.getString("name"));
            roster.setPosition(jObject.getString("position"));
            rosterList.add(i, roster);
            i++;
        }   

这是日志消息

08-09 01:03:09.703: I/School FTB Cache(18328): [{"position":"DB","id":"1","name":"Ne'Quan Phillips"},{"position":"DB","id":"2","name":"Tony Grimes"},{"position":"LB","id":"3","name":"TJ Taimatuia"},{"position":"LB","id":"4","name":"Steven Lakalaka"},{"position":"WR","id":"5","name":"Billy Ray StutzMaan"}]
08-09 01:03:09.703: W/System.err(18328): java.lang.NullPointerException
08-09 01:03:09.757: W/System.err(18328):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-09 01:03:09.757: W/System.err(18328):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:87)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:103)
08-09 01:03:09.773: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:135)
08-09 01:03:09.773: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:1)
08-09 01:03:09.781: W/System.err(18328):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:87)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:103)
08-09 01:03:09.796: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:135)
08-09 01:03:09.796: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:1)
08-09 01:03:09.796: W/System.err(18328):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-09 01:03:09.796: W/System.err(18328):    at java.lang.Thread.run(Thread.java:1019)
08-09 01:03:09.812: W/System.err(18328):    at java.lang.Thread.run(Thread.java:1019)
4

2 回答 2

2

问题是这一行:

JSONArray rosterJsonArray = new JSONArray(line);

一旦你的 while 循环完成,line 等于 null。因此new JSONArray(line)抛出 NullPointerException。加上你的 while 循环line每次都会重置,所以line = line + "\n";什么也不做。您应该创建另一个字符串以在循环文件时放入这些行。

于 2013-08-08T19:59:07.107 回答
-1

尝试更改此行

String line = null;

对此

String line; // or this //String line = "";

还在 try 块之外对其进行初始化(使其在函数内部成为全局或局部的

于 2013-08-08T20:14:57.107 回答