我正在编写要访问的文件的JSON,以便从我的应用程序中消除尽可能多的网络访问/延迟。我之前没有处理过将文件写入内存的问题,并从Google的文档中获取了大部分的读/写代码,但我仍然遇到问题。
目前,我在这一行得到一个空指针异常:
FileOutputStream fOut = openFileOutput("runesInfo.json", Context.MODE_PRIVATE)
这让我相信文件永远不会被创建,或者我错误地初始化它,但我不确定如何。
完整的代码部分:
public class GetRunes extends AsyncTask<String, String, String> {
boolean runesCached = false;
protected String doInBackground(String[] runeId) {
String url = "https://prod.api.pvp.net/api/lol/static-data/" + region + "/v1.2/rune?api_key=" + api_key;
JSONParser jsonParser = new JSONParser();
JSONObject runeInfo = jsonParser.getJSONFromUrl(url);
String jsonString = runeInfo.toString();
String readJson = null;
Context context = GetStaticData.this;
if(!runesCached) {
Log.d("Cache", "Caching File");
try {
File file = new File(context.getFilesDir(), "runesInfo.json");
FileOutputStream fOut = openFileOutput("runesInfo.json", Context.MODE_PRIVATE);
fOut.write(jsonString.getBytes());
fOut.close();
Log.d("Cache", "Cache Complete");
runesCached = true;
} catch (Exception e){
e.printStackTrace();
}
}
String name = null;
try {
FileInputStream fis = openFileInput("runesInfo.json");
InputStreamReader isr = new InputStreamReader(fis);
fis.read(readJson.getBytes());
JSONObject storedJson = new JSONObject(readJson);
Log.d("Stored JSON", "" + storedJson);
JSONObject idJson = storedJson.getJSONObject("data");
JSONObject single = idJson.getJSONObject(runeId[0]);
try {
name = single.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
}
logcat的:
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats D/Cache﹕ Caching File
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ java.lang.NullPointerException
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.content.ContextWrapper.getFilesDir(ContextWrapper.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.lang.Thread.run(Thread.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ java.util.concurrent.ExecutionException: java.lang.NullPointerException
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.report(FutureTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.get(FutureTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.get(AsyncTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData.getRuneInfo(GetStaticData.jav
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/dalvikvm﹕ threadid=20: thread exiting with uncaught exception (group=0x41650e18)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.RunesActivity$GetRunes.onPostExecute(RunesA
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.RunesActivity$GetRunes.onPostExecute(RunesA
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.Looper.loop(Looper.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ Caused by: java.lang.NullPointerException
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
04-29 09:53:58.239 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
04-29 09:53:58.239 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.Thread.run(Thread.java)
答案 0 :(得分:0)
我建议使用android Service。我认为,它将最适合您的需求。只需将代码移动到Service,摆脱静态上下文,在onStartCommand方法上执行AsyncTask,并在需要保存JSON时调用startService。
答案 1 :(得分:0)
File file = new File(context.getFilesDir(), "runesInfo.json");
FileOutputStream fOut = openFileOutput("runesInfo.json", Context.MODE_PRIVATE);
您创建了一个文件实例,但从未在流中使用它。
FileOutputStream fOut = new FileOutputStream(new File(context.getFilesDir(), "runesInfo.json"));
应该做的工作。