设备离线时显示最新数据(android studio)

时间:2018-09-05 22:21:11

标签: android json android-studio listview caching

您好,JSON非常新。我有一些任务要从服务器http://jsonplaceholder.typicode.com/posts检索包含JSON数据的数据。我已经完成检索数据并显示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;
}
}

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

0 个答案:

没有答案