瀏覽代碼

优化请求方法

YanYi 7 年之前
父節點
當前提交
b3c760cf18

+ 1 - 1
README.md

@@ -4,7 +4,7 @@
 
 module 下添加
 
-     compile 'com.github.love-yanyi:OkHttp:1.0.9'
+     compile 'com.github.love-yanyi:OkHttp:1.1.1'
 
 <br/>
 project 下添加

+ 3 - 3
okhttp2/src/main/java/com/mylove/okhttp/AutoRequest.java

@@ -25,12 +25,12 @@ public class AutoRequest {
             synchronized (AutoRequest.class) {
                 if (instance == null) {
                     instance = new AutoRequest();
-                    mContext = context;
-                    url = str;
-                    requestType = type;
                 }
             }
         }
+        mContext = context;
+        url = str;
+        requestType = type;
         return instance;
     }
 

+ 17 - 16
okhttp2/src/main/java/com/mylove/okhttp/DownloadCall.java

@@ -29,33 +29,30 @@ import rx.Subscriber;
 class DownloadCall {
     @SuppressLint("StaticFieldLeak")
     private static Context mContext;
-    private String url;
-    private Subscriber<? super ResultMsg> subscriber;
-    private Call call;
+    private static String url;
+    private static Subscriber<? super ResultMsg> subscriber;
+    private static Call call;
     @SuppressLint("StaticFieldLeak")
     private static DownloadCall instance;
-    private CallType callType;
+    private static CallType callType;
     private static OkHttpClient okHttpClient;
     private String fileName;
 
-    private DownloadCall(String url, Request request, Subscriber<? super ResultMsg> subscriber, CallType callType) {
-        this.url = url;
-        this.subscriber = subscriber;
-        this.call = okHttpClient.newCall(request);
-        this.callType = callType;
+    private DownloadCall() {
+
     }
 
     /**
      * okHttpClient初始化,并添加拦截及缓存
      *
-     * @param context    上下文
-     * @param mCacheUrl  缓存地址
-     * @param request    请求
-     * @param subscriber 返回
-     * @param callType   请求类型
+     * @param context     上下文
+     * @param str         缓存地址
+     * @param request     请求
+     * @param subscriber1 返回
+     * @param type        请求类型
      * @return
      */
-    static DownloadCall getInstance(Context context, String mCacheUrl, Request request, Subscriber<? super ResultMsg> subscriber, CallType callType) {
+    static DownloadCall getInstance(Context context, String str, Request request, Subscriber<? super ResultMsg> subscriber1, CallType type) {
         if (instance == null) {
             synchronized (DownloadCall.class) {
                 if (instance == null) {
@@ -67,10 +64,14 @@ class DownloadCall {
                             .connectTimeout(30, TimeUnit.SECONDS)
                             .readTimeout(30, TimeUnit.SECONDS)
                             .build();
-                    instance = new DownloadCall(mCacheUrl, request, subscriber, callType);
+                    instance = new DownloadCall();
                 }
             }
         }
+        url = str;
+        subscriber = subscriber1;
+        call = okHttpClient.newCall(request);
+        callType = type;
         return instance;
     }
 

+ 3 - 3
okhttp2/src/main/java/com/mylove/okhttp/DownloadObservable.java

@@ -51,12 +51,12 @@ class DownloadObservable {
             synchronized (DownloadObservable.class) {
                 if (instance == null) {
                     instance = new DownloadObservable();
-                    mContext = context;
-                    requestType = type1;
-                    callType = type2;
                 }
             }
         }
+        mContext = context;
+        requestType = type1;
+        callType = type2;
         return instance;
     }
 

+ 3 - 3
okhttp2/src/main/java/com/mylove/okhttp/DownloadRequest.java

@@ -25,12 +25,12 @@ public class DownloadRequest {
             synchronized (DownloadRequest.class) {
                 if (instance == null) {
                     instance = new DownloadRequest();
-                    mContext = context;
-                    url = str;
-                    requestType = type;
                 }
             }
         }
+        mContext = context;
+        url = str;
+        requestType = type;
         return instance;
     }
 

+ 3 - 3
okhttp2/src/main/java/com/mylove/okhttp/ObservableRequest.java

@@ -37,12 +37,12 @@ class ObservableRequest {
             synchronized (ObservableRequest.class) {
                 if (instance == null) {
                     instance = new ObservableRequest();
-                    mContext = context;
-                    requestType = type1;
-                    callType = type2;
                 }
             }
         }
+        mContext = context;
+        requestType = type1;
+        callType = type2;
         return instance;
     }
 

+ 25 - 23
okhttp2/src/main/java/com/mylove/okhttp/OkCall.java

@@ -25,36 +25,33 @@ import rx.Subscriber;
 class OkCall {
     @SuppressLint("StaticFieldLeak")
     private static Context mContext;
-    private String mCacheUrl;
-    private Subscriber<? super ResultMsg> subscriber;
-    private Call call;
+    private static String mCacheUrl;
+    private static Subscriber<? super ResultMsg> subscriber;
+    private static Call call;
     @SuppressLint("StaticFieldLeak")
     private static OkCall instance;
-    private CallType callType;
+    private static CallType callType;
     private static OkHttpClient okHttpClient;
 
-    private OkCall(String mCacheUrl, Request request, Subscriber<? super ResultMsg> subscriber, CallType callType) {
-        this.mCacheUrl = mCacheUrl;
-        this.subscriber = subscriber;
-        this.call = okHttpClient.newCall(request);
-        this.callType = callType;
+    private OkCall() {
+
     }
 
     /**
      * okHttpClient初始化,并添加拦截及缓存
      *
-     * @param context    上下文
-     * @param mCacheUrl  缓存地址
-     * @param request    请求
-     * @param subscriber 返回
-     * @param callType   请求类型
+     * @param context     上下文
+     * @param cacheUrl    缓存地址
+     * @param request     请求
+     * @param subscriber1 返回
+     * @param type        请求类型
      * @return
      */
-    public static OkCall getInstance(Context context, String mCacheUrl, Request request, Subscriber<? super ResultMsg> subscriber, CallType callType) {
-//        if (instance == null) {
-//            synchronized (OkCall.class) {
-//                if (instance == null) {
-                    mContext = context;
+    public static OkCall getInstance(Context context, String cacheUrl, Request request, Subscriber<? super ResultMsg> subscriber1, CallType type) {
+        mContext = context;
+        if (instance == null) {
+            synchronized (OkCall.class) {
+                if (instance == null) {
                     OkHttpClient httpClient = new OkHttpClient();
                     okHttpClient = httpClient.newBuilder()
                             .addNetworkInterceptor(new CacheInterceptor())
@@ -62,13 +59,18 @@ class OkCall {
                             .connectTimeout(30, TimeUnit.SECONDS)
                             .readTimeout(30, TimeUnit.SECONDS)
                             .build();
-                    instance = new OkCall(mCacheUrl, request, subscriber, callType);
-//                }
-//            }
-//        }
+                    instance = new OkCall();
+                }
+            }
+        }
+        mCacheUrl = cacheUrl;
+        subscriber = subscriber1;
+        call = okHttpClient.newCall(request);
+        callType = type;
         return instance;
     }
 
+
     /**
      * 请求
      */

+ 1 - 1
okhttp2/src/main/java/com/mylove/okhttp/OkHttpUtil.java

@@ -21,10 +21,10 @@ public class OkHttpUtil {
             synchronized (OkHttpUtil.class) {
                 if (instance == null) {
                     instance = new OkHttpUtil();
-                    mContext = context;
                 }
             }
         }
+        mContext = context;
         return instance;
     }