首页 > 解决方案 > 设备离线时显示最新数据(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;
}
}

为什么我的代码不起作用?我应该如何处理我的代码?请给我一些帮助。非常感谢您..

标签: androidjsonandroid-studiolistviewcaching

解决方案


推荐阅读