Nate
Nate

Reputation: 412

Storing and reading JSON from internal memory

I'm having troubles writing to memory, and from there accessing it. I believe I am writing it correctly, but when reading it, I'm getting a null pointer exception at this section of code:

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

in the read portion of my code. I haven't dealt with saving data to a local file before, so I'm really not sure what I could be doing wrong. If anyone could point me in the right direction, I'd appreciate it.

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

Upvotes: 0

Views: 1939

Answers (1)

vyncjob
vyncjob

Reputation: 21

Storing and retrieving information from files on the internal storage of an android device can be found here: Storage Options | Android Developer

Luckily a lot of the work is shown here: Parsing JSON from InputStream

So to read a JSON Object from an internal file:

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();

To write you also need to specify the file MODE, advised to use MODE_PRIVATE. If a file with the given name does not yet exist it will be created if possible, otherwise a FileNotFoundException will be thrown.

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

Upvotes: 1

Related Questions