【问题标题】:saving JSON file from url in Internal Storage从内部存储中的 url 保存 JSON 文件
【发布时间】: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()]);

但它不会打开文件以读取它

【问题讨论】:

标签: java android


【解决方案1】:
  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();
        }

  }

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      【解决方案3】:
      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

      【讨论】:

        猜你喜欢
        • 2012-03-16
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2017-03-21
        • 2012-10-21
        相关资源
        最近更新 更多