6.請求網絡步驟

操做步驟都是:加載本地數據——若是沒有請求服務器——服務器請求完保存數據——本地數據有了或者保存完數據了去解析數據
FileUtils 
  
  
  
  
public class FileUtils { public static final String CACHE = "cache"; public static final String ICON = "icon"; public static final String ROOT = "GooglePlay"; /** * 獲取圖片的緩存的路徑 * * @return */ public static File getIconDir() { return getDir(ICON); } /** * 獲取緩存路徑 * * @return */ public static File getCacheDir() { return getDir(CACHE); } public static File getDir(String cache) { StringBuilder path = new StringBuilder(); if (isSDAvailable()) { path.append(Environment.getExternalStorageDirectory() .getAbsolutePath()); path.append(File.separator);// '/' path.append(ROOT);// /mnt/sdcard/GooglePlay path.append(File.separator); path.append(cache);// /mnt/sdcard/GooglePlay/cache } else { File filesDir = UiUtils.getContext().getCacheDir(); // cache // getFileDir // file path.append(filesDir.getAbsolutePath());// /data/data/com.itheima.googleplay/cache path.append(File.separator);// /data/data/com.itheima.googleplay/cache/ path.append(cache);// /data/data/com.itheima.googleplay/cache/cache } File file = new File(path.toString()); if (!file.exists() || !file.isDirectory()) { file.mkdirs();// 建立文件夾 } return file; } private static boolean isSDAvailable() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } }}
BaseProtocol (協議) 
   
   
   
   
public abstract class BaseProtocol<T> { public T load(int index) { // 加載本地數據 String json = loadLocal(index); if (json == null) { // 請求服務器 json = loadServer(index); if (json != null) { saveLocal(json, index); } } if (json != null) { return paserJson(json); } else { return null; } } private String loadServer(int index) { HttpResult httpResult = HttpHelper.get(HttpHelper.URL +getKey()//請求網絡,寫xutils也行 + "?index=" + index); String json = httpResult.getString(); return json; } private void saveLocal(String json, int index) { BufferedWriter bw = null; try { File dir=FileUtils.getCacheDir(); //在第一行寫一個過時時間 File file = new File(dir, getKey()+"_" + index); // /mnt/sdcard/googlePlay/cache/home_0 FileWriter fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(System.currentTimeMillis() + 1000 * 100 + "");//若是數字過時了從新請求網絡 bw.newLine();// 換行 bw.write(json);// 把整個json文件保存起來 bw.flush(); bw.close(); } catch (Exception e) { e.printStackTrace(); }finally{ IOUtils.closeQuietly(bw);//關流 } } private String loadLocal(int index) { // 若是發現文件已通過期了 就不要再去複用緩存了 File dir=FileUtils.getCacheDir();// 獲取緩存所在的文件夾 File file = new File(dir, getKey()+"_" + index); try { FileReader fr=new FileReader(file); BufferedReader br=new BufferedReader(fr); long outOfDate = Long.parseLong(br.readLine()); if(System.currentTimeMillis()>outOfDate){ return null; }else{ String str=null; String sw=new StringWriter(); while((str=br.readLine())!=null){ sw.write(str); } return sw.toString(); } } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解析json * @param json * @return */ public abstract T paserJson(String json); /** * 說明了關鍵字 * @return */ public abstract String getKey();}
子類的請求網絡只須要關心這倆個方法就好了
附件裏有三個http請求訪問的類,之後能夠直接拿來用,比較方便
HttpHelper裏是訪問的主要代碼
HttpRetry裏是返回的結果





附件列表

相關文章
相關標籤/搜索