我正在尝试将字符串中的 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)