2013-10-11 2 views
1

내 안드로이드 애플 리케이션을 위해 나는 URL에서 안드로이드의 내부 스토리지로 JSON을 다운로드하고 그것을 읽어야합니다. 내가 여기에 몇 가지 문제가 있지만 가장 좋은 방법은 내장 스토리지로 바이트 []로 저장 할 수 있다고 생각하는 것은 내가 지금까지 내부 저장소에 URL에서 JSON 파일 저장

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()]); 

를 작성했지만 그것을 읽기 위해 파일을 열 수 없습니다 것입니다

+0

당신은 파일에 확장자를 추가하려고 할 수 있을까? –

+0

나는 이것을 시도했지만 여전히 – Libathos

+0

과 같은 파일이 생성 되었습니까? –

답변

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

    } 
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에 캐스팅.

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; 
} 
관련 문제