Hitesh Matnani
Hitesh Matnani

Reputation: 553

How to perform caching in android?

I am building an articles reading android application like TechChurn. I am fetching data from server in the form of json.

I am parsing Id(unique),title, author name and articles-content from json and displaying it in list-view.

Those parsed content is stored in local for accessing without internet access.

This i have done using a cache function. Here is my code that is using for caching -

public final class CacheThis {
private CacheThis() {

}

public static void writeObject(Context context, String fileName,
        Object object) throws IOException {
    FileOutputStream fos;
    ObjectOutputStream oos;
    if (fileExistance(fileName, context)) {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE
                | Context.MODE_APPEND);
        oos = new AppendingObjectOutputStream(fos);
    } else {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE
                | Context.MODE_APPEND);
        oos = new ObjectOutputStream(fos);
    }
    oos.writeObject(object);
    oos.flush();
    oos.close();

    fos.close();
}

public static List<Object> readObject(Context context, String fileName) {
    List<Object> list = new ArrayList<Object>(0);
    FileInputStream fis;
    try {

        fis = context.openFileInput(fileName);

        ObjectInputStream ois = new ObjectInputStream(fis);
        Object object;
        try {
            while (true) {
                object = ois.readObject();
                list.add(object);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        fis.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return list;
}

public static boolean fileExistance(String fname, Context context) {
    File file = context.getFileStreamPath(fname);
    return file.exists();
}

}

my article should be cached based on id instead its been loaded for every-time when app is started

Upvotes: 2

Views: 205

Answers (1)

Sripathi
Sripathi

Reputation: 1780

Use the following methods to store and retrieve the data.. Here you can store the object..

private void writeData(Object data, String fileName) {
        try {
            FileOutputStream fos = context.openFileOutput(fileName,
                    Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(data);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public Object readData(String fileName){
        Object data = null;
        if (context != null) {
            try {
                FileInputStream fis = context.openFileInput(fileName);
                ObjectInputStream is = new ObjectInputStream(fis);
                data = is.readObject();
                is.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

Write the data once you got the response from the server(at first request to the server). Use the id as file name. After that check for the particular file before you want to hit server for data. If the file is available then you can get the data from that file, otherwise hit the server.

Upvotes: 1

Related Questions