{"contents":[{"productID":34,"productName":"SuperWidget"},{"productID":56,"productName":"WonderWidget"}]}
從遠端拉取的資料
private void saveFile() {
try {
FileOutputStream fileOutputStream = openFileOutput("123.txt",
Context.MODE_PRIVATE);
fileOutputStream.write(resource.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(JSONExampleActivity.this, "儲存成功", Toast.LENGTH_LONG).show();
}
儲存在Internal Storage
之後讀取
private void readFile() {
String temp = null;
try {
FileInputStream fileInputStream = openFileInput("123.txt");
int length = fileInputStream.available();
byte[] buffer = new byte[length];
fileInputStream.read(buffer);
fileInputStream.close();
temp = new String(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
請問readFile()內的temp 要如何才會被這段執行呢?
JSONArray test = temp.getJSONArray("contents");
我知道宣告錯誤
我也試過把它變成JSONObject型態 但是都還是錯誤 請高人指點 謝謝 已爬過多文