从内部存储器存储和读取JSON

时间:2014-04-29 02:24:35

标签: android

我在写入内存时遇到了麻烦,并从那里访问它。我相信我写得正确,但在阅读时,我在这部分代码中得到一个空指针异常:

fis = new FileInputStream(getFilesDir() + "/" + "runeInfo");

在我的代码的读取部分。我之前没有处理过将数据保存到本地文件的问题,所以我真的不确定自己可能做错了什么。如果有人能指出我正确的方向,我会很感激。

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;

            if(!runesCached) {
                Log.d("Cache", "Caching File");

                try {
                    FileWriter fstream;
                    BufferedWriter out;

                    fstream = new FileWriter(getFilesDir() + "/" + "runeInfo");
                    out = new BufferedWriter(fstream);
                    out.write(String.valueOf(jsonString.getBytes()));
                    out.close();

                } catch (Exception e){}
                Log.d("Cache", "Cache Complete");
                runesCached = true;
            }

            String name = null;
            try {
                FileInputStream fis;

                fis = new FileInputStream(getFilesDir() + "/" + "runeInfo");

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

1 个答案:

答案 0 :(得分:1)

可以在此处找到从Android设备内部存储上的文件存储和检索信息:Storage Options | Android Developer

幸运的是,这里展示了很多工作:Parsing JSON from InputStream

所以从内部文件中读取JSON对象:

String fileName = "Enter your file name here";
FileInputStream fis = openFileInput(fileName); 

InputStreamReader isr = new InputStreamReader(fis);
BufferedReader streamReader = new BufferedReader(isr);

StringBuilder responseStrBuilder = new StringBuilder();

String inputStr;
while ((inputStr = streamReader.readLine()) != null)
    responseStrBuilder.append(inputStr);
JSONObject jsonobj = new JSONObject(responseStrBuilder.toString());
streamReader.close();
isr.close();
fis.close();

要写你还需要指定文件MODE,建议使用MODE_PRIVATE。如果具有给定名称的文件尚不存在,则将尽可能创建,否则将抛出FileNotFoundException。

String fileName = "Enter your file name here";
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
...
fos.close();