我正在尝试将String中的Json Response存储为Text文件中的Cache。但是当我从文本文件中读取它时,我无法解析它。
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)
答案 0 :(得分:2)
问题在于这一行:
JSONArray rosterJsonArray = new JSONArray(line);
while循环完成后, line等于null 。所以new JSONArray(line)
会抛出NullPointerException。另外,while循环每次都会重置line
,因此line = line + "\n";
不执行任何操作。您应该创建另一个String,以便在循环浏览文件时将行放入。
答案 1 :(得分:-1)
尝试更改此行
String line = null;
到这个
String line; // or this //String line = "";
也在try块之外初始化它(在函数内部使其成为全局或局部的
)