6.请求网络步骤

操作步骤都是:加载本地数据——如果没有请求服务器——服务器请求完保存数据——本地数据有了或者保存完数据了去解析数据
FileUtils 
  1. public class FileUtils {
  2. public static final String CACHE = "cache";
  3. public static final String ICON = "icon";
  4. public static final String ROOT = "GooglePlay";
  5. /**
  6. * 获取图片的缓存的路径
  7. *
  8. * @return
  9. */
  10. public static File getIconDir() {
  11. return getDir(ICON);
  12. }
  13. /**
  14. * 获取缓存路径
  15. *
  16. * @return
  17. */
  18. public static File getCacheDir() {
  19. return getDir(CACHE);
  20. }
  21. public static File getDir(String cache) {
  22. StringBuilder path = new StringBuilder();
  23. if (isSDAvailable()) {
  24. path.append(Environment.getExternalStorageDirectory()
  25. .getAbsolutePath());
  26. path.append(File.separator);// '/'
  27. path.append(ROOT);// /mnt/sdcard/GooglePlay
  28. path.append(File.separator);
  29. path.append(cache);// /mnt/sdcard/GooglePlay/cache
  30. } else {
  31. File filesDir = UiUtils.getContext().getCacheDir(); // cache
  32. // getFileDir
  33. // file
  34. path.append(filesDir.getAbsolutePath());// /data/data/com.itheima.googleplay/cache
  35. path.append(File.separator);// /data/data/com.itheima.googleplay/cache/
  36. path.append(cache);// /data/data/com.itheima.googleplay/cache/cache
  37. }
  38. File file = new File(path.toString());
  39. if (!file.exists() || !file.isDirectory()) {
  40. file.mkdirs();// 创建文件夹
  41. }
  42. return file;
  43. }
  44. private static boolean isSDAvailable() {
  45. if (Environment.getExternalStorageState().equals(
  46. Environment.MEDIA_MOUNTED)) {
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. }
BaseProtocol (协议) 
  1. public abstract class BaseProtocol<T> {
  2. public T load(int index) {
  3. // 加载本地数据
  4. String json = loadLocal(index);
  5. if (json == null) {
  6. // 请求服务器
  7. json = loadServer(index);
  8. if (json != null) {
  9. saveLocal(json, index);
  10. }
  11. }
  12. if (json != null) {
  13. return paserJson(json);
  14. } else {
  15. return null;
  16. }
  17. }
  18. private String loadServer(int index) {
  19. HttpResult httpResult = HttpHelper.get(HttpHelper.URL +getKey()//请求网络,写xutils也行
  20. + "?index=" + index);
  21. String json = httpResult.getString();
  22. return json;
  23. }
  24. private void saveLocal(String json, int index) {
  25. BufferedWriter bw = null;
  26. try {
  27. File dir=FileUtils.getCacheDir();
  28. //在第一行写一个过期时间
  29. File file = new File(dir, getKey()+"_" + index); // /mnt/sdcard/googlePlay/cache/home_0
  30. FileWriter fw = new FileWriter(file);
  31. bw = new BufferedWriter(fw);
  32. bw.write(System.currentTimeMillis() + 1000 * 100 + "");//如果数字过期了重新请求网络
  33. bw.newLine();// 换行
  34. bw.write(json);// 把整个json文件保存起来
  35. bw.flush();
  36. bw.close();
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }finally{
  40. IOUtils.closeQuietly(bw);//关流
  41. }
  42. }
  43. private String loadLocal(int index) {
  44. // 如果发现文件已经过期了 就不要再去复用缓存了
  45. File dir=FileUtils.getCacheDir();// 获取缓存所在的文件夹
  46. File file = new File(dir, getKey()+"_" + index);
  47. try {
  48. FileReader fr=new FileReader(file);
  49. BufferedReader br=new BufferedReader(fr);
  50. long outOfDate = Long.parseLong(br.readLine());
  51. if(System.currentTimeMillis()>outOfDate){
  52. return null;
  53. }else{
  54. String str=null;
  55. String sw=new StringWriter();
  56. while((str=br.readLine())!=null){
  57. sw.write(str);
  58. }
  59. return sw.toString();
  60. }
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. return null;
  64. }
  65. }
  66. /**
  67. * 解析json
  68. * @param json
  69. * @return
  70. */
  71. public abstract T paserJson(String json);
  72. /**
  73. * 说明了关键字
  74. * @return
  75. */
  76. public abstract String getKey();
  77. }
子类的请求网络只需要关心这俩个方法就行了
附件里有三个http请求访问的类,以后可以直接拿来用,比较方便
HttpHelper里是访问的主要代码
HttpRetry里是返回的结果





附件列表

    原文地址:https://www.cnblogs.com/sixrain/p/4969213.html