OkHttpClient.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.edufound.reader.util;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Environment;
  6. import androidx.annotation.Nullable;
  7. import com.edufound.reader.receiver.ShowPageLoadingReceiver;
  8. import com.okhttplib.HttpInfo;
  9. import com.okhttplib.OkHttpUtil;
  10. import com.okhttplib.annotation.CacheType;
  11. import com.okhttplib.annotation.Encoding;
  12. import com.okhttplib.callback.BaseCallback;
  13. import com.okhttplib.cookie.PersistentCookieJar;
  14. import com.okhttplib.cookie.cache.SetCookieCache;
  15. import com.okhttplib.cookie.persistence.SharedPrefsCookiePersistor;
  16. import java.io.File;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. public class OkHttpClient {
  20. //初始化OKHTTP
  21. private static String downloadFileDir = Environment.getExternalStorageDirectory().getPath() + "/okHttp_download/";
  22. private static String cacheDir = Environment.getExternalStorageDirectory().getPath() + "/okHttp_cache";
  23. private static Map<String, String> mHeaders;
  24. public static void initOkHttpUtil(Context context) {
  25. OkHttpUtil.init(context)
  26. .setConnectTimeout(10)//连接超时时间
  27. .setWriteTimeout(300)//写超时时间
  28. .setReadTimeout(300)//读超时时间
  29. .setMaxCacheSize(10 * 1024 * 1024)//缓存空间大小
  30. .setCacheType(CacheType.FORCE_NETWORK)//缓存类型
  31. .setHttpLogTAG("HttpLog")//设置请求日志标识
  32. .setIsGzip(false)//Gzip压缩,需要服务端支持
  33. .setShowHttpLog(true)//显示请求日志
  34. .setShowLifecycleLog(false)//显示Activity销毁日志
  35. .setRetryOnConnectionFailure(false)//失败后不自动重连
  36. .setCachedDir(new File(cacheDir))//设置缓存目录
  37. .setDownloadFileDir(downloadFileDir)//文件下载保存目录
  38. .setResponseEncoding(Encoding.UTF_8)//设置全局的服务器响应编码
  39. .setRequestEncoding(Encoding.UTF_8)//设置全局的请求参数编码
  40. // .setHttpsCertificate("12306.cer")//设置全局Https证书
  41. .addResultInterceptor(HttpInterceptor.ResultInterceptor)//请求结果拦截器
  42. .addExceptionInterceptor(HttpInterceptor.ExceptionInterceptor)//请求链路异常拦截器
  43. .setCookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)))//持久化cookie
  44. .build();
  45. refHeader();
  46. }
  47. private static void refHeader() {
  48. if (mHeaders == null) {
  49. mHeaders = new HashMap<>();
  50. }
  51. mHeaders.clear();
  52. mHeaders.put("uid", Consts.getUID());
  53. mHeaders.put("channelCode", Consts.getUMengChannel());
  54. }
  55. public static void doGetAsync(Activity activity, HttpInfo.Builder info, BaseCallback callback) {
  56. if (activity.getLocalClassName().contains("LoginQRAlertActivity")) {
  57. //登录界面
  58. if (!info.build().getUrl().contains("/user/isLogin")) {
  59. //不是二维码一直查找是否登录,才出现loading
  60. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  61. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  62. }
  63. } else {
  64. if (!info.build().getUrl().contains("posts/list")
  65. && !info.build().getUrl().contains("/userRead/check")
  66. && !info.build().getUrl().contains("/order/info/")) {
  67. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  68. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  69. }
  70. }
  71. refHeader();
  72. OkHttpUtil.getDefault(activity).doGetAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  73. }
  74. public static void doGetAsync(HttpInfo.Builder info, BaseCallback callback) {
  75. if (!info.build().getUrl().contains("posts/list")
  76. && !info.build().getUrl().contains("/userRead/check")
  77. && !info.build().getUrl().contains("/order/info/")) {
  78. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  79. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  80. }
  81. refHeader();
  82. OkHttpUtil.getDefault().doGetAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  83. }
  84. public static void doPostAsync(@Nullable Activity activity, HttpInfo.Builder info, BaseCallback callback) {
  85. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  86. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  87. refHeader();
  88. OkHttpUtil.getDefault(activity).doPostAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  89. }
  90. public static void doPostAsync(HttpInfo.Builder info, BaseCallback callback) {
  91. if (!info.build().getUrl().contains("playLog")) {
  92. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  93. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  94. }
  95. refHeader();
  96. OkHttpUtil.getDefault().doPostAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  97. }
  98. public static void doPutAsync(Activity activity, HttpInfo.Builder info, BaseCallback callback) {
  99. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  100. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  101. refHeader();
  102. OkHttpUtil.getDefault(activity).doPutAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  103. }
  104. public static void doDeleteAsync(Activity activity, HttpInfo.Builder info, BaseCallback callback) {
  105. Intent intent = new Intent(ShowPageLoadingReceiver.PAGE_LOADING_SHOW);
  106. Consts.getmApplicAtion().getApplicationContext().sendBroadcast(intent);
  107. refHeader();
  108. OkHttpUtil.getDefault(activity).doDeleteAsync(info.addHeads(mHeaders).build(), new EfunboxCallBack(callback));
  109. }
  110. public static void doUploadFile(Activity activity, HttpInfo.Builder info) {
  111. refHeader();
  112. OkHttpUtil.getDefault(activity).doUploadFileAsync(info.addHeads(mHeaders).build());
  113. }
  114. }