android - Storing a file to internal storage and reading it -
What I am trying to do is store a JSON file as a string in internal storage This is done to access it later. The logic behind this is not to reach the server on every request, because this data is stable. Once it has been archived, it will not have to be retrieved, unless there are some updates. File storage I have done something before, and I was hoping someone could hand me my current code is throwing an empty pointer exception on this line:
file file = new File (getFilesDir (), filename);
My code:
protected string doInBackground (string [] runeId) {String url = "https://prod.api.pvp.net/ Api / lol / static-data / "+ region +" /v1.2/rune/ "+ runeId [0] +"? Api_key = "+ api_key; JSONParser jsonParser = new JSONParser (); JSONObject runeInfo = jsonParser.getJSONFromUrl (url); String jsonString = runeInfo.toString (); String filename = "runeinfo"; File file = new file (getFilesDir (), filename); String readjustment = null; If (RunesCached!) {Log D. ("Cache", "caching file"); {FileOutputStream OS = Try New FileOutputStream (File); Os.write (jsonString.getBytes ()); Os.close (); Log D. ("Cache", "cache full"); RunesCached = true; } Hold (FileNotFoundException E) {// TODO Auto generated blocking block e.printStackTrace (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); }} String name = null; {FileInputStream fis; Stored file RuneInfo = new file (getFilesDir (), filename); Fis = New FileInputStream (storedRuneInfo); Fis.read (readJson.getBytes ()); JSONObject archived JS = new JSONObject (readJson); Try {name = storedJson.getString ("name"); } Hold (JasonX Express E) {e.printStackTrace (); }} Hold (FileNotFoundException e) {e.printStackTrace (); } Hold (Streamcorporated Expression E) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Hold (JasonX Express E) {e.printStackTrace (); } Return name; }}
Try it instead,:
File file = new file (getFilesDir (.) ToString (), filename);
getFilesDir () returns a file, not a string, which takes the file class creator as a parameter.
getFilesDir () toString () should be returned as something / edit / data / data / com.your.app /
Edit:
This is a Only gives error about how:
try {FileWriter fstream; Take out the buffer; Fstream = New freeware (getFilesDir () + "/" + "Filename"); Outside = new buffed water (FSTM); Out.write (jsonString.getBytes ()); Out.close (); } Hold (exception e) {}
Comments
Post a Comment