您好,登錄后才能下訂單哦!
Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
Android目前很多同學(xué)使用Volley請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù),但是Volley沒(méi)有對(duì)請(qǐng)求過(guò)得數(shù)據(jù)進(jìn)行緩存,因此需要我們自己手動(dòng)緩存。 一下就是我的一種思路,僅供參考
具體使用方法為:
HashMap<String,String> params = new HashMap<>(); params.put("id", "1"); params.put("user", "mcoy"); new NetWorkHelper(getActivity()).jacksonMethodRequest ("method_id", params, new TypeReference<ReturnTemple<FirstCategories>>(){}, handler, msgId);
NetWorkHelper---對(duì)Volley的封裝,首先調(diào)用CacheManager.get(methodName, params);方法獲取緩存中的數(shù)據(jù),如果數(shù)據(jù)為null,
則繼續(xù)發(fā)送網(wǎng)絡(luò)請(qǐng)求。
/** * @version V1.0 網(wǎng)絡(luò)請(qǐng)求幫助類(lèi) * @author: mcoy */ public final class NetWorkHelper { private NetWorkManager netWorkUtils; public NetWorkHelper(Context context){ netWorkUtils = new NetWorkManager(context); } public static final String COMMON_ERROR_MSG = "連接超時(shí),請(qǐng)稍后重試"; public static final int COMMON_ERROR_CODE = 2; /** * 使用Jackson請(qǐng)求的方法 * @param methodName * @param params * @param handler * @param msgId */ public void jacksonMethodRequest(final String methodName,final HashMap<String,String> params,TypeReference javaType, final Handler handler,final int msgId){ ResponseListener listener = new ResponseListener(){ @Override public void onResponse(Object response, boolean isCache) { PrintLog.log(response.toString()); if (isCache){ CacheManager.put(methodName, params, response); } if (handler != null) { Message message = handler.obtainMessage(); message.what = msgId; message.obj = response; handler.sendMessage(message); } } }; Object respone = CacheManager.get(methodName, params); if(respone != null){ listener.onResponse(respone,false); return; } HashMap<String,String> allParams = Config.setSign(true); allParams.putAll(params); Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); if (handler != null) { Message message = handler.obtainMessage(); message.what = msgId; message.obj = COMMON_ERROR_MSG; handler.sendMessage(message); } } }; netWorkUtils.jacksonRequest(getUrl(methodName), allParams,javaType, listener, errorListener); } /** * Url直接請(qǐng)求 * @param url * @param params * @param handler * @param msgId */ public void urlRequest(String url,HashMap<String,String> params,JsonParser jsonParser,final Handler handler,final int msgId){ request(url, true, params, jsonParser, handler, msgId); } /** * 通過(guò)方法請(qǐng)求 * @param methodName 方法名 * @param params 請(qǐng)求參數(shù) * @param jsonParser Json解析器 * @param handler 回調(diào)通知 * @param msgId 通知的Id */ public void methodRequest(String methodName, final HashMap<String,String> params,final JsonParser jsonParser,final Handler handler,final int msgId){ request(getUrl(methodName),true,params,jsonParser,handler,msgId); } /** * 通過(guò)方法請(qǐng)求 * @param methodName 方法名 * @param params 請(qǐng)求參數(shù) * @param jsonParser Json解析器 * @param handler 回調(diào)通知 * @param msgId 通知的Id */ public void methodRequest(String methodName, boolean isLogin,final HashMap<String,String> params,final JsonParser jsonParser,final Handler handler,final int msgId){ request(getUrl(methodName),isLogin,params,jsonParser,handler,msgId); } private void request(final String url, boolean isLogin,final HashMap<String,String> params,final JsonParser jsonParser,final Handler handler,final int msgId){ final HashMap<String,String> allParams = Config.setSign(isLogin); allParams.putAll(params); Response.Listener listener = new Response.Listener<String>() { @Override public void onResponse(String response) { /** * 有些請(qǐng)求默認(rèn)是沒(méi)有parser傳過(guò)來(lái)的,出參只求String,譬如聯(lián)合登錄等 * 所以加了一個(gè)else if */ Object result; PrintLog.log(response); if (jsonParser != null ) { jsonParser.json2Obj(response); jsonParser.temple.description = jsonParser.temple.getResultDescription(); result = jsonParser.temple; } else { ReturnTemple temple = new ReturnTemple(); temple.issuccessful = false; temple.description = COMMON_ERROR_MSG; temple.result = -100; result = temple; } if (handler != null) { Message message = handler.obtainMessage(); message.what = msgId; message.obj = result; handler.sendMessage(message); } } }; Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Object result; if (jsonParser != null) { ReturnTemple temple = new ReturnTemple(); temple.issuccessful = false; temple.description = COMMON_ERROR_MSG; temple.result = COMMON_ERROR_CODE; result = temple; } else { result = COMMON_ERROR_MSG; } if (handler != null) { Message message = handler.obtainMessage(); message.what = msgId; message.obj = result; handler.sendMessage(message); } } }; netWorkUtils.request(url, allParams, listener, errorListener); } /** * 根據(jù)訪(fǎng)求名拼裝請(qǐng)求的Url * @param methodName * @return */ private String getUrl(String methodName){ String url = Config.getUrl(); if (!StringUtil.isNullOrEmpty(methodName)) { url = url + "?method=" + methodName; } return url; } }
CacheManager---將針對(duì)某一method所請(qǐng)求的數(shù)據(jù)緩存到本地文件當(dāng)中,主要是將CacheRule寫(xiě)到本地文件當(dāng)中
/** * @version V1.0 <緩存管理> * @author: mcoy */ public final class CacheManager { /** * 一個(gè)方法對(duì)應(yīng)的多個(gè)Key,比如分類(lèi)都是同一個(gè)方法,但是請(qǐng)求會(huì)不一樣,可能都要緩存 */ private static HashMap<String, ArrayList<String>> methodKeys; private static final String keyFileName = "keys.pro"; /** * 讀取緩存的Key */ public static void readCacheKey() { methodKeys = (HashMap<String, ArrayList<String>>) readObject(keyFileName); if (methodKeys == null) { methodKeys = new HashMap<String, ArrayList<String>>(); } } /** * 保存緩存 */ public static void put(String method, HashMap<String, String> params, Object object) { long expireTime = CacheConfig.getExpireTime(method); if (expireTime <= 0 || methodKeys == null) {//有效時(shí)間小于0,則不需要緩存 return; } String key = createKey(method, params); if (methodKeys.containsKey(method)) { ArrayList<String> keys = methodKeys.get(method); keys.add(key); } else { ArrayList<String> keys = new ArrayList<>(); keys.add(key); methodKeys.put(method, keys); } writeObject(methodKeys, keyFileName); String fileName = key + ".pro"; CacheRule cacheRule = new CacheRule(expireTime, object); LogModel.log(" put " + method + " " + key + " " + cacheRule); writeObject(cacheRule, fileName); } public static Object get(String method, HashMap<String, String> params) { long expireTime = CacheConfig.getExpireTime(method); if (expireTime <= 0 || methodKeys == null || !methodKeys.containsKey(method)) {//有效時(shí)間小于0,則不需要緩存 return null; } ArrayList<String> keys = methodKeys.get(method); // String saveKey = keys.get(method); String key = createKey(method, params); String fileName = key + ".pro"; // LogModel.log("get"+method+" "+(saveKey.equals(key))+" path:"+fileName); CacheRule cacheRule = null; try { if (keys.contains(key)) { cacheRule = (CacheRule) readObject(fileName); } } catch (Exception e) { e.printStackTrace(); } LogModel.log("get :" + method + " " + key + " " + cacheRule); if (cacheRule != null && cacheRule.isExpire()) { return cacheRule.getData(); } else { return null; } } public static void main(String[] args) { String method = "category.getCategory"; HashMap<String, String> params = new HashMap<>(); params.put("categoryId", "-3"); System.out.println(createKey(method, params)); System.out.println(CacheRule.getCurrentTime()); } /** * 生成Key * * @param method 請(qǐng)求的方法名 * @param params 私有參數(shù)(除公共參數(shù)以外的參數(shù)) * @return */ private static String createKey(String method, HashMap<String, String> params) { try { MessageDigest digest = MessageDigest.getInstance("md5"); digest.digest(method.getBytes("UTF-8")); StringBuilder builder = new StringBuilder(method); if (params != null && params.size() > 0) { Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); builder.append(entry.getKey()).append("=").append(entry.getValue()); } } byte[] tempArray = digest.digest(builder.toString().getBytes("UTF-8")); StringBuffer keys = new StringBuffer(); for (byte b : tempArray) { // 與運(yùn)算 int number = b & 0xff;// 加鹽 String str = Integer.toHexString(number); // System.out.println(str); if (str.length() == 1) { keys.append("0"); } keys.append(str); } return keys.toString().toUpperCase(); } catch (Exception e) { e.printStackTrace(); } return method.toUpperCase(); } /** * 將對(duì)象寫(xiě)到文件中 * * @param object * @param fileName */ private static void writeObject(Object object, String fileName) { try { ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File(CacheConfig.getCachePath() + fileName))); oo.writeObject(object); oo.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 讀取對(duì)象 * * @param fileName * @return */ private static Object readObject(String fileName) { Object result = null; try { File file = new File(CacheConfig.getCachePath() + fileName); if (file.exists()) { ObjectInputStream oi = new ObjectInputStream(new FileInputStream(file)); result = oi.readObject(); oi.close(); } } catch (Exception e) { e.printStackTrace(); } return result; } }
CacheConfig---初始化哪些方法需要做緩存處理,以及緩存的有效時(shí)間
/** * @version V1.0 <設(shè)置哪些類(lèi)數(shù)據(jù)需要緩存> * @author: mcoy */ public final class CacheConfig { /**方法對(duì)應(yīng)的緩存有效時(shí)間,時(shí)間是毫秒*/ private static HashMap<String,Long> methodExpireTimes = new HashMap<String, Long>(); private static String cachePath = null; static { methodExpireTimes.put(ConstMethod.GET_CATEGORY_LIST,30 * 60 * 1000L); methodExpireTimes.put(ConstMethod.GET_NEW_CATEGORY_LIST,30 * 60 * 1000L); } /** * 初始化緩存路徑 * @param context */ public static void init(Context context){ cachePath = context.getFilesDir().getPath()+ File.separator+"cache"+File.separator; File file = new File(cachePath); if(!file.exists()){ file.mkdirs(); } CacheManager.readCacheKey(); } /**緩存路徑*/ public static String getCachePath() { return cachePath; } /** * 獲取有方法對(duì)應(yīng)的有效時(shí)間,如果方法沒(méi)有添加緩存或者緩存時(shí)間小于0,則不添加緩存 * @param method * @return */ public static long getExpireTime(String method){ if(methodExpireTimes.containsKey(method)){ return methodExpireTimes.get(method); }else { return -1; } } }
CacheRule---主要有兩個(gè)參數(shù),expireTime需要緩存的時(shí)間, data需要緩存的數(shù)據(jù)
public class CacheRule implements Serializable{ /** 有效時(shí)間 */ public long expireTime; /** 緩存時(shí)間*/ public long cacheTime; /** 緩存數(shù)據(jù) */ private Object data; public CacheRule(long expireTime,Object data){ cacheTime = getCurrentTime(); this.expireTime = expireTime; this.data = data; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } @Override public int hashCode() { return BeanTools.createHashcode(expireTime, cacheTime, data); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("expireTime:").append(expireTime).append(" cacheTime:").append(cacheTime) .append(" curTime:").append(getCurrentTime()) .append(" isExpire:").append(isExpire()).append(" data:").append(data==null?"null":data.toString()); return builder.toString(); } /** * 數(shù)據(jù)是否有效 * @return */ public boolean isExpire(){ long curTime = getCurrentTime(); return curTime>(expireTime+cacheTime)?false:true; } /** * 獲取當(dāng)前時(shí)間 * @return */ public static long getCurrentTime(){ // if (Build.VERSION_CODES.JELLY_BEAN_MR1 <= Build.VERSION.SDK_INT) { // return SystemClock.elapsedRealtimeNanos(); // } else { return System.currentTimeMillis(); // } } }
NetWorkManager---往RequestQueue中添加JacksonRequest請(qǐng)求,然后Volley會(huì)去請(qǐng)求數(shù)據(jù)
/** * 網(wǎng)絡(luò)請(qǐng)求的工具類(lèi) */ public final class NetWorkManager { private RequestQueue requestQueue ; public NetWorkManager(Context context){ requestQueue = Volley.newRequestQueue(context); } /** * 使用Jackson解析請(qǐng)求的方法 * @param url * @param params * @param javaType 成功時(shí)返回的Java類(lèi)型 * @param listener * @param errorListener */ public void jacksonRequest(final String url,final HashMap<String,String> params,TypeReference javaType, ResponseListener listener, Response.ErrorListener errorListener){ JacksonRequest jacksonRequest = new JacksonRequest(url,javaType,params,listener,errorListener); requestQueue.add(jacksonRequest); } /** * 普通的網(wǎng)絡(luò)請(qǐng)求,返回的Json * @param url * @param params * @param listener * @param errorListener */ public void request(final String url,final HashMap<String,String> params,Response.Listener listener,Response.ErrorListener errorListener){ StringRequest stringRequest = new StringRequest(Request.Method.POST,url,listener,errorListener){ @Override protected Map<String, String> getParams() throws AuthFailureError { if (PrintLog.DEBUG) { Iterator<Map.Entry<String,String>> iterator = params.entrySet().iterator(); StringBuilder builder = new StringBuilder(url+" "); while (iterator.hasNext()){ Map.Entry<String,String> entry = iterator.next(); builder.append(entry.getKey()+":"+entry.getValue()).append("; "); } PrintLog.log(builder.toString()); } return params; } }; requestQueue.add(stringRequest); } }
JacksonRequest---繼承Request,重寫(xiě)deliverResponse方法,并調(diào)用ResponseListener的onResponse方法,并通過(guò)CacheManager.put(methodName, params, response);將獲取的response緩存到CacheManager中。這一步很重要,調(diào)用我們自己的listener,而不是Volley提供給我們的Response.Listener
/** * Created by mcoy */ public class JacksonRequest extends Request { private final HashMap<String,String> params; private final ResponseListener listener; private final ObjectMapper mapper; private final TypeReference javaType; public JacksonRequest(String url,TypeReference javaType,HashMap<String,String> params,ResponseListener listener,Response.ErrorListener errorListener){ super(Method.POST,url,errorListener); mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,true); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.params = params; this.listener = listener; this.javaType = javaType; } @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } @Override protected Response parseNetworkResponse(NetworkResponse response) { String json; try { json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); PrintLog.log("返回的json:" + json); return Response.success(mapper.readValue(json,javaType), HttpHeaderParser.parseCacheHeaders(response)); }catch (UnsupportedEncodingException e){ json = new String(response.data); PrintLog.log("json:"+json); try { return Response.success(mapper.readValue(json,javaType),HttpHeaderParser.parseCacheHeaders(response)); } catch (IOException e1) { return Response.error(new ParseError(e)); } } catch (JsonParseException e) { PrintLog.log(e.toString()); return Response.error(new ParseError(e)); } catch (JsonMappingException e) {PrintLog.log(e.toString()); return Response.error(new ParseError(e)); } catch (IOException e) {PrintLog.log(e.toString()); return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(Object response) { listener.onResponse(response,true); } }
ResponseListener---自定義的一個(gè)listener接口, 在發(fā)送請(qǐng)求時(shí),需要將其實(shí)現(xiàn)。其中才參數(shù)中比Volley的提供的listener過(guò)了一個(gè)isCache的Boolean值,根據(jù)此值來(lái)決定是否要緩存。
/** * @version V1.0 <描述當(dāng)前版本功能> * @author: mcoy */ public interface ResponseListener<T> { public void onResponse(T response, boolean isCache); }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。