【发布时间】:2019-05-01 16:34:15
【问题描述】:
我正在使用 volley 从 Internet 获取文件,该文件是一个数组。我将文件保存在缓存中为此我必须将文件转换为字符串。我有一个读取缓存并将响应传递给另一个文件以显示的函数 信息,但是当我尝试将 resoionse 转换回数组时出现错误
Value AuthStatus of type java.lang.String cannot be converted to JSONObject
我真的是 android 新手,希望有人能指出我正确的方向
private void cacheFile(JSONObject response) {
JSONObject res = response;
String filename = "jsonfile";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(res.toString().getBytes("utf-8"));
outputStream.close();
Log.e(TAG, "Bien");
} catch (Exception e) {
e.printStackTrace();
}
}
private void readCache(String filename) {
FileInputStream inputStream;
try {
inputStream = openFileInput(filename);
inputStream.read();
String body = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
inputStream.close();
fromCache(body);
} catch (Exception e) {
e.printStackTrace();
}
}
public void fromCache(String json) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("cars");
Log.e(TAG, "Array Size: " + jsonArray.length());
} catch (JSONException e) {
e.printStackTrace();
}
}
【问题讨论】: