【发布时间】:2013-11-20 19:58:45
【问题描述】:
对于我的 android 应用程序,我需要将 JSON 从 url 下载到 android 的内部存储中,然后从中读取。我认为将它作为 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()]);
但它不会打开文件以读取它
【问题讨论】:
-
您可以尝试将扩展名添加到文件中吗?
-
我试过了,还是一样
-
文件创建了吗?
-
是的,我可以从 ddms 资源管理器中看到