Advertisement

智慧北京:网络访问数据缓存

阅读量:

因为每一次打开应用前都能加载上一次存储的内容, 所以此次将重点设定如何实现网络访问数据的持久化存储功能. 实现目的: 在初次使用该应用时会自动完成数据缓存, 在再次使用时若发现有已缓存的数据, 则会立即加载这些数据, 并在2分钟后切换回最新的网络更新.

这里写图片描述
这里写图片描述

1、在 CacheUtils设置缓存string、long等数据。
CacheUtils.java

复制代码
    package huaxa.it.zhihuidemo.utils;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    /** * @项目名: ZhiHuiDemo
     * @包名: huaxa.it.zhihuidemoUtils
     * @类名: CacheUtils
     * @创建者: 黄夏莲
     * @创建时间: 2016年10月3日 ,下午6:09:45
     * * @描述:跳转页面
     * */
    public class CacheUtils
    {
    private final static String         sharedPreferencesName   = "zhbj";
    private static SharedPreferences    mpreferences;                       // 静态的,从内存取(只需取1次),比读文件快(每次都要读取)
    
    private static SharedPreferences getsp(Context context)
    {
        if (mpreferences == null)
        {
            mpreferences = context.getSharedPreferences(sharedPreferencesName,
                    Context.MODE_PRIVATE);
        }
        return mpreferences;
    
    }
    
    /** * 通过sp获得boolean类型的数据,没有默认为false
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @return
     */
    public static boolean getBoolean(Context context, String key)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
    
        mpreferences = getsp(context);
        return mpreferences.getBoolean(key, false);// 第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
    }
    
    /** * 通过sp获得boolean类型的数据
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @param defValue
     *            :默认值
     * @return
     */
    public static boolean getBoolean(Context context, String key,
            boolean defValue)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        return mpreferences.getBoolean(key, defValue);
    }
    
    /** * * 设置Boolean的缓存数据
     * * @param context
     *            :上下文
     * @param key
     *            :缓存对应的key
     * @param value
     *            :缓存对应的值
     * @return
     */
    public static void setBoolean(Context context, String key, boolean value)
    {
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        Editor editor = mpreferences.edit();// 获取编辑器
        editor.putBoolean(key, value);
        editor.commit();
    
    }
    
    /** * 通过sp获得String类型的数据,没有默认为null
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @return
     */
    public static String getString(Context context, String key)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
    
        mpreferences = getsp(context);
        return mpreferences.getString(key, null);// 第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
    }
    
    /** * 通过sp获得String类型的数据
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @param defValue
     *            :默认值
     * @return
     */
    public static String getString(Context context, String key, String defValue)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        return mpreferences.getString(key, defValue);
    }
    
    /** * * 设置String的缓存数据
     * * @param context
     *            :上下文
     * @param key
     *            :缓存对应的key
     * @param value
     *            :缓存对应的值
     * @return
     */
    public static void setString(Context context, String key, String value)
    {
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        Editor editor = mpreferences.edit();// 获取编辑器
        editor.putString(key, value);
        editor.commit();
    
    }
    
    /** * 通过sp获得long类型的数据,没有默认为-1
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @return
     */
    public static long getLong(Context context, String key)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
    
        mpreferences = getsp(context);
        return mpreferences.getLong(key, -1);// 第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
    }
    
    /** * 通过sp获得long类型的数据
     * * @param context
     *            :上下文
     * @param key
     *            :存储的key
     * @param defValue
     *            :默认值
     * @return
     */
    public static long getLong(Context context, String key, long defValue)
    {
        // TODO Auto-generated method stub
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        return mpreferences.getLong(key, defValue);
    }
    
    /** * * 设置long的缓存数据
     * * @param context
     *            :上下文
     * @param key
     *            :缓存对应的key
     * @param value
     *            :缓存对应的值
     * @return
     */
    public static void setLong(Context context, String key, long value)
    {
        // SharedPreferences mpreferences = context.getSharedPreferences(
        // sharedPreferencesName, Context.MODE_PRIVATE);
        mpreferences = getsp(context);
        Editor editor = mpreferences.edit();// 获取编辑器
        editor.putLong(key, value);
        editor.commit();
    
    }
    }

NewsCenterTabController.java

复制代码
    public void initData()
    {
        ……
        final String url = Constans.NewsCenter_URI;
    
        // 到缓存中取数据
        String json = CacheUtils.getString(mContext, url);
        long date = CacheUtils.getLong(mContext, url +"_date"); 
        // 判断json是否为空
        if (!TextUtils.isEmpty(json))
        {
            //有缓存数据
            //从缓存中取出数据
            Log.i(TAG, "读取到缓存");
            ProcessData(json);
            //过后2分钟开始进行网络加载数据
            if((System.currentTimeMillis() - date) < DELAY_TIME ){
                Log.i(TAG, "使用缓存数据,不使用网络");
                return;
            }
        }
    
        ……
        util.send(HttpMethod.GET, url, new RequestCallBack<String>()
        {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo)
            {
                // 响应状态码,Success指的是访问服务器成功,返回状态码与服务器有关而不一定是200,返回状态码不确定。
                // 这个状态码要和提供的状态码匹配,访问接口才会成功。
                // int statusCode = responseInfo.statusCode;
                // 访问接口成功(此处先默认是匹配的)
                String result = responseInfo.result;
    
                Log.i(TAG, "设置缓存数据");
                //设置缓存数据和时间
                CacheUtils.setString(mContext, url, result);
                CacheUtils.setLong(mContext, url +"_date", System.currentTimeMillis());
    
                Log.i(TAG, "访问接口成功" + result);
                // 数据的处理
                ProcessData(result);
            }
    
        ……
        });

全部评论 (0)

还没有任何评论哟~