2

for my android app i need to download a JSON from a url into android's internal storage and then read from it. I think that the best way to save it as byte[] into internal storage although i have some problems here is what i've written so far

File storage = new File("/sdcard/appData/photos");
storage.mkdirs();

JSONParser jParser = new JSONParser();

// getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url1);

//transforming jsonObject to byte[] and store it
String jsonString = json.toString();                
byte[] jsonArray = jsonString.getBytes();

String filen = "jsonData";
File fileToSaveJson = new File("/sdcard/appData",filen);

FileOutputStream fos;
fos = new FileOutputStream(fileToSaveJson);

fos = openFileOutput(filen,Context.MODE_PRIVATE);
fos.write(jsonArray);
fos.close();


//reading jsonString from storage and transform it into jsonObject              
FileInputStream fis;
File readFromJson = new File("/sdcard/appData/jsonData");

fis =  new FileInputStream(readFromJson);

fis =  new FileInputStream(readFromJson);
InputStreamReader isr = new InputStreamReader(fis);

fis.read(new byte[(int)readFromJson.length()]);

but it won't open the file in order to read it

6

3 Answers 3

3
  private void downloadAndStoreJson(String url,String tag){

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(url);          

        String jsonString = json.toString();                
        byte[] jsonArray = jsonString.getBytes();

        File fileToSaveJson = new File("/sdcard/appData/LocalJson/",tag);



        BufferedOutputStream bos;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(fileToSaveJson));
            bos.write(jsonArray);
            bos.flush();
            bos.close();

        } catch (FileNotFoundException e4) {
            // TODO Auto-generated catch block
            e4.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            jsonArray=null;
            jParser=null;
            System.gc();
        }

  }
0
1
public static File createCacheFile(Context context, String fileName, String json) {
    File cacheFile = new File(context.getFilesDir(), fileName);
    try {
        FileWriter fw = new FileWriter(cacheFile);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();

        // on exception null will be returned
        cacheFile = null;
    }

    return cacheFile;
}

public static String readFile(File file) {
    String fileContent = "";
    try {
        String currentLine;
        BufferedReader br = new BufferedReader(new FileReader(file));

        while ((currentLine = br.readLine()) != null) {
            fileContent += currentLine + '\n';
        }

        br.close();
    } catch (IOException e) {
        e.printStackTrace();

        // on exception null will be returned
        fileContent = null;
    }
    return fileContent;
}
0
public void writeObjectInInternalStorage(Context context, String filename, Object object) throws IOException {
    FileOutputStream fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
    objectOutputStream.writeObject(object);
    objectOutputStream.close();
    fileOutputStream.close();
}

public Object readObjectFromInternalStorage(Context context, String filename) throws IOException, FileNotFoundException, ClassNotFoundException{
    FileInputStream fileInputStream = context.openFileInput(filename);
    return new ObjectInputStream(fileInputStream).readObject();
}

use these methods, call readObjectFromInternalStorage() and cast it to JSON String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.