我正在尝试创建一个应用程序,他们将一些Json数据保存在本地文件中(在SD卡上)..
第一次运行时我只需将json数据保存在文件中......
然后返回'applications_arr'(不是来自文件,而是来自JsonReader)
请参阅holde asyncTask-class here:
并ApplicationsModel.class(实现Parcelable)
amFile = new File(this.activity.getFilesDir()+"/applications"+UUID.randomUUID()+".json");
gson = new GsonBuilder().create();
reader = new JsonReader(new BufferedReader(new InputStreamReader(new URL("http://software-center.ubuntu.com/api/2.0/applications/any/ubuntu/any/any/").openConnection().getInputStream())));
reader.beginArray();
//save am-json-data on sdcard
FileWriter fw = new FileWriter(amFile.getAbsoluteFile());
fw.write(gson.toJson(reader, ApplicationsModel.class));
fw.close();
//create am-arraylist to use now.. amFile is other use
while (reader.hasNext()) {
ApplicationsModel model = gson.fromJson(reader, ApplicationsModel.class);
applications_arr.add(model);
}
reader.endArray();
reader.close();
看起来错误与:
fw.write(gson.toJson(reader,ApplicationsModel.class));
我的Logcat看起来像this。
答案 0 :(得分:0)
您可能需要在WRITE_EXTERNAL_STORAGE中指定manifest权限。
这看起来像是:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
编辑:
查看您的logcat,错误是:
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException:类型的预期接收者 com.ubuntu.apps.ApplicationsModel,不是 com.google.gson.stream.JsonReader
这就是说Gson期望在ApplicationModel中读取,而是给你一个JsonReader。
那就是说,你试图保存你正在读取从URL流到文件的Json的行不需要Gson。只需将Json直接传送到文件即可。