1

我正在尝试让应用程序将一些 Json 数据保存在本地文件中(在 sdcard 上)..

第一次运行时,我只需将 json-data 保存在一个文件中......

然后返回“applications_arr”(不是来自文件,而是来自 JsonReader)

在这里查看holde asyncTask-class :

还有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 看起来像这样

4

1 回答 1

0

You probably need to specify the WRITE_EXTERNAL_STORAGE permission in your manifest.

This would look like:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

EDIT:

Looking at your logcat, the error is:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: expected receiver of type com.ubuntu.apps.ApplicationsModel, not com.google.gson.stream.JsonReader

Which is saying that Gson is expecting to read in an ApplicationModel, and instead you are giving it a JsonReader.

That said, the line where you are trying to save the Json you're reading streaming from the URL to a file doesn't need Gson. Just stream the Json straight to the file.

于 2012-12-26T01:02:12.280 回答