Browse Source

提交2.0.9测试版本1

kth_win_android 2 years ago
parent
commit
f2d859f3a3

+ 2 - 1
README.md

@@ -1,6 +1,7 @@
 # BaseOkHttp
 
 ![](https://img.shields.io/badge/okhttplib-2.0.9-green)
+![](https://img.shields.io/badge/okhttplib-2.0.9-base1-red)
 
 
 OkHttp请求封装
@@ -30,7 +31,7 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.9'
 <dependency>
   <groupId>com.yanyi.benyanyi</groupId>
   <artifactId>okhttplib</artifactId>
-  <version>2.0.8</version>
+  <version>2.0.9</version>
   <type>aar</type>
 </dependency>
 ~~~

+ 3 - 3
app/src/main/java/com/mylove/okhttp/DownloadActivity.java

@@ -82,7 +82,7 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
 //                });
 //                break;
             case R.id.main_btn_down2:
-                OkHttpUtil.getInstance(mContext).url(url).download(true).start(new OnDownLoadObserver() {
+                OkHttpUtil.getInstance().url(url).download(true).start(new OnDownLoadObserver() {
                     @Override
                     public void onNext(DownloadInfo downloadInfo) {
                         Jlog.v(downloadInfo.getProgress());
@@ -126,10 +126,10 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
 //                OkHttpUtil.getInstance(mContext).url(url1).download().cancel();
                 break;
             case R.id.main_btn_cancel2:
-                OkHttpUtil.getInstance(mContext).url(url).download(true).pause();
+                OkHttpUtil.getInstance().url(url).download(true).pause();
                 break;
             case R.id.main_btn_cancel3:
-                OkHttpUtil.getInstance(mContext).url(url).download(true).cancel();
+                OkHttpUtil.getInstance().url(url).download(true).cancel();
                 break;
             default:
                 break;

+ 2 - 2
app/src/main/java/com/mylove/okhttp/DownloadManager.java

@@ -27,11 +27,11 @@ public class DownloadManager {
     }
 
     public void start(String url) {
-        OkHttpUtil.getInstance(context).url(url).download(".apk").start();
+        OkHttpUtil.getInstance().url(url).download(".apk").start();
     }
 
     public void cancel(String url) {
-        OkHttpUtil.getInstance(context).url(url).download().cancel();
+        OkHttpUtil.getInstance().url(url).download().cancel();
     }
 
 }

+ 4 - 4
app/src/main/java/com/mylove/okhttp/MainActivity.java

@@ -200,7 +200,7 @@ public class MainActivity extends AppCompatActivity {
                 Jlog.d(entry.getKey() + "=" + entry.getValue());
             }
         }
-        OkHttpUtil.getInstance(this).url("https://mp.idimage.com.cn/expo-bossapi/studentInfo/upload").postMultipart(null, oMap).async(new OnOkHttpListener<Object>() {
+        OkHttpUtil.getInstance().url("https://mp.idimage.com.cn/expo-bossapi/studentInfo/upload").postMultipart(null, oMap).async(new OnOkHttpListener<Object>() {
             @Override
             public void onCompleted() {
 
@@ -239,7 +239,7 @@ public class MainActivity extends AppCompatActivity {
                 Jlog.d(entry.getKey() + "=" + (entry.getValue().toString().substring(0, 100)));
             }
         }
-        OkHttpUtil.getInstance(this).url("https://opend.icaiji.com.cn/wxapp/v6/photo").postText(null, oMap).async(new OnOkHttpListener<Object>() {
+        OkHttpUtil.getInstance().url("https://opend.icaiji.com.cn/wxapp/v6/photo").postText(null, oMap).async(new OnOkHttpListener<Object>() {
             @Override
             public void onCompleted() {
 
@@ -307,7 +307,7 @@ public class MainActivity extends AppCompatActivity {
         Map<String, Object> bodyMap = new HashMap<>();
         bodyMap.put("data", "+fAFfMCGsMZqeS+gyrZm1A==");
         Jlog.v(bodyMap);
-        OkHttpUtil.getInstance(this).url(url, true).postText(headerMap, bodyMap).async(new OnOkHttpListener<Object>() {
+        OkHttpUtil.getInstance().url(url, true).postText(headerMap, bodyMap).async(new OnOkHttpListener<Object>() {
             @Override
             public void onCompleted() {
 
@@ -330,7 +330,7 @@ public class MainActivity extends AppCompatActivity {
         String url = "http://apitest.yanyi.online/app/xiangyou.apk";
 //        String filePath = "/dectector/dfu/";
 ////        String filePath = Environment.getExternalStorageDirectory().toString() + "/dectector/dfu/";
-        OkHttpUtil.getInstance(this).url(url).download(true).start(new OnDownLoadObserver() {
+        OkHttpUtil.getInstance().url(url).download(true).start(new OnDownLoadObserver() {
             @Override
             public void onComplete() {
 

+ 34 - 4
okhttplib/src/main/java/com/benyanyi/okhttp/OkHttpUtil.java

@@ -1,5 +1,6 @@
 package com.benyanyi.okhttp;
 
+import android.app.Application;
 import android.content.Context;
 
 import com.benyanyi.okhttp.config.HttpRequest;
@@ -7,6 +8,7 @@ import com.benyanyi.okhttp.type.RequestType;
 import com.benyanyi.okhttp.util.FormatUtil;
 
 import java.io.File;
+import java.lang.reflect.InvocationTargetException;
 
 /**
  * @author YanYi
@@ -17,15 +19,13 @@ import java.io.File;
 public class OkHttpUtil {
 
     private static OkHttpUtil instance;
-    private static Context mContext;
 
     public static File cacheFile;
 
-    public static OkHttpUtil getInstance(Context context) {
+    public static OkHttpUtil getInstance() {
         if (instance == null) {
             instance = new OkHttpUtil();
         }
-        mContext = context;
         return instance;
     }
 
@@ -33,14 +33,44 @@ public class OkHttpUtil {
         if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
             throw new RuntimeException("url is not http or https");
         }
-        return new HttpRequest(mContext, url, false);
+        return new HttpRequest(getApplication(), url, false);
+    }
+
+    public RequestType url(Context context, String url) {
+        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
+            throw new RuntimeException("url is not http or https");
+        }
+        return new HttpRequest(context, url, false);
     }
 
     public RequestType url(String url, boolean isCache) {
+        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
+            throw new RuntimeException("url is not http or https");
+        }
+        return new HttpRequest(getApplication(), url, isCache);
+    }
+
+    public RequestType url(Context mContext, String url, boolean isCache) {
         if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
             throw new RuntimeException("url is not http or https");
         }
         return new HttpRequest(mContext, url, isCache);
     }
 
+    private Application getApplication() {
+        Application application;
+        try {
+            application = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeException(e);
+        } catch (InvocationTargetException e) {
+            throw new RuntimeException(e);
+        } catch (NoSuchMethodException e) {
+            throw new RuntimeException(e);
+        } catch (ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
+        return application;
+    }
+
 }

+ 2 - 2
okhttplib/src/main/java/com/benyanyi/okhttp/config/CacheInterceptor.java

@@ -43,7 +43,7 @@ class CacheInterceptor implements Interceptor {
             int maxAge = 0 * 60;
             // 有网络时 设置缓存超时时间0个小时
             response.newBuilder()
-                    .header("Cache-Control", "public, max-age=$maxAge")
+                    .header("Cache-Control", "public, max-age=" + maxAge)
                     .removeHeader("Pragma")// 清除头信息,因为服务器如果不支持,会返回一些干扰信息,不清除下面无法生效
                     .build();
         } else {
@@ -51,7 +51,7 @@ class CacheInterceptor implements Interceptor {
             int maxStale = 60 * 60 * 24 * 28;
 //            val maxStale = 0
             response.newBuilder()
-                    .header("Cache-Control", "public, only-if-cached, max-stale=$maxStale")
+                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                     .removeHeader("Pragma")
                     .build();
         }

+ 0 - 6
okhttplib/src/main/java/com/benyanyi/okhttp/config/HttpConfig.java

@@ -41,12 +41,6 @@ class HttpConfig {
     static okhttp3.Cache privateCache(Context mContext) {
         return new okhttp3.Cache(mContext.getCacheDir(), 10240 * 10240);
     }
-//    fun verifier(): HostnameVerifier
-//
-//    {
-//        return HostnameVerifier { _, _ -> true }
-//
-//    }
 
     static HostnameVerifier verifier() {
         return new HostnameVerifier() {