怎样才能使一个缓存类的Andr​​oid? [英] How can make a cache class in android?

查看:156
本文介绍了怎样才能使一个缓存类的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个阅读的文章像TechChurn android应用。我从服务器以JSON形式获取数据。

我解析题目,作者姓名和JSON文章内容,并在列表视图显示它。

这条加载保存都在我的本地文件,并在我的应用程序加载的时候没有互联网连接。

这我已经使用缓存类来完成。

现在假设我有我的应用程序负载50的文章和我的应用程序处于联机模式意味着有互联网服务,现在我打开我的应用程序一个人又在离线模式意味着没有互联网服务数据进行缓存,并显示从本地缓存50篇文件中。

现在,我面临的问题是,如果我的应用程序又是开放的在在线模式。
它加载初始计量10篇文章,这10篇都已经从previous 50篇本地文件拷贝过去,现在本地文件只有10篇文章。

这意味着,如果我现在运行我在离线模式下的应用程序。

这将只显示10条,因为这50文章由10篇文章加载。
这里是我的缓存类 -

 包缓存;
公共类CacheThis {私人无效CacheThis(){}公共静态无效的writeObject(上下文的背景下,字符串文件名,Object对象)抛出
 IOException异常{
      FOS的FileOutputStream = context.openFileOutput(文件名,Context.MODE_PRIVATE);
      ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);
      oos.writeObject(对象);
      oos.flush();
      oos.close();      fos.close();
   }   公共静态对象的readObject(上下文的背景下,字符串文件名)抛出IOException异常,
         ClassNotFoundException的{
      FIS的FileInputStream = context.openFileInput(文件名);
      ObjectInputStream的OIS =新的ObjectInputStream(FIS);
      Object对象= ois.readObject();
      fis.close();
      返回对象;
   }
   }


解决方案

我觉得你的情况更好地使用数据库来保存离线模式下的数据。我preferred ORMLite:
http://ormlite.com/sqlite_java_android_orm.shtml
在这种情况下,你不需要打开和读取文件,每一个项目每一次的JSON解析。和土特产品将与对象模型的工作,而不是与生JSON。

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

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

This loaded article are save in my local file and are loaded in my application when there is no internet access.

This i have done using a cache class.

Now suppose i have load 50 articles in my app and my app is in online mode means there is internet services now i open my app one's again in offline mode means no internet services the data is cached and show 50 articles from local cache file.

Now the problem that i face is that if my app is open one's again in online mode. It load 10 articles in initially stated and this 10 articles are been copy past in local file from previous 50 articles and now the local file has only 10 articles.

which means that if i run my app in offline mode now.

It will show only 10 article because that 50 articles are been loaded by 10 articles. Here is my cache class -

package cache;
public class CacheThis {

private void 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;
   }
   }

解决方案

I think in your case better to use database for save data for offline mode. I preferred ORMLite: http://ormlite.com/sqlite_java_android_orm.shtml In that case you don't need to open and read file, parse json every time for every item. And tou will work with object model, not with raw json.

这篇关于怎样才能使一个缓存类的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆