[英]saving JSON file from url in Internal Storage
for my android app i need to download a JSON from a url into android's internal storage and then read from it. 对于我的android应用程序,我需要从url下载JSON到android的内部存储,然后从中读取。 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
我认为将它另存为byte []到内部存储的最好方法是,尽管我在这里遇到了一些问题,但这是我到目前为止写的
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 但它不会打开文件以进行读取
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;
}
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();
}
}
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
. 使用这些方法,调用
readObjectFromInternalStorage()
并将其readObjectFromInternalStorage()
为JSON String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.