android - 设备离线时显示最新数据(android studio)
问题描述
你好,我对 JSON 很陌生。我有一些任务要从包含 JSON 数据的服务器http://jsonplaceholder.typicode.com/posts检索数据。我已完成检索数据并显示 listView 中的所有数据。我想让我的应用程序可以处理没有互联网连接的情况,应用程序可以显示最新数据。我已经尝试过缓存,但它仍然无法工作。缓存未从列表视图中保存我的数据,因此它不显示任何内容。这是我的代码:
private void getData() {
if (InternetConnection.getInstance().isOnline(MainActivity.this)) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
PostModel postModel = new PostModel();
postModel.setTitle(jsonObject.getString("title"));
postModel.setId(jsonObject.getInt("id"));
postModel.setBody(jsonObject.getString("body"));
postList.add(postModel);
try {
cacheThis.writeObject(MainActivity.this, "savePost", postList);
} catch (IOException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
} else {
try {
postList.addAll((List<PostModel>) cacheThis.readObject(MainActivity.this, "savePost"));
if (postList.size()<0) {
postList.addAll((List<PostModel>) cacheThis.readObject(MainActivity.this, "savePost"));
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "PULL TO REFRESH", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
}
这是缓存类:
package com.example.meita.fetchjson;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
final class cacheThis {
private cacheThis() {
}
public static void writeObject(Context context, String fileName, Object object) throws IOException {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.flush();
oos.close();
fos.close();
}
public static Object readObject(Context context, String fileName) throws IOException,
ClassNotFoundException {
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
fis.close();
return object;
}
}
为什么我的代码不起作用?我应该如何处理我的代码?请给我一些帮助。非常感谢您..
解决方案
推荐阅读
- glfw - Mingw-w64 的 GLFW 库
- python - 如何在 Python 中访问 groupcollide() 返回的列表中的每个项目属性?
- firebase-authentication - Firebase 不接受从 Google Identity Platform 的 REST API 获取的 JWT:“自定义令牌格式不正确。”
- r - 如何通过R包中的字符串调用内部数据集
- python - Pandas 在另一列 B 中为 A 列中的选定值设置条目
- reactjs - React Native - 模态 - 动态最大高度
- reactjs - 在个人选项卡中打开 React 组件
- python - 为什么终端给我这个错误并且所有模块都被导入了?
- android - SSO 谷歌登录无法登录
- .htaccess - 重写规则不适用于 apache2,lucee tomcat