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

4

3 回答 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();
        }

  }
于 2013-11-20T11:58:45.643 回答
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;
}
于 2013-10-11T10:27:20.380 回答
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();
}

使用这些方法,调用readObjectFromInternalStorage()并将其转换为JSON String.

于 2013-10-11T10:25:38.610 回答