YanYi преди 7 години
родител
ревизия
7dc78ba51d

+ 1 - 1
README.md

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

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

@@ -77,6 +77,78 @@ public class MainActivity extends AppCompatActivity {
     }
 
     private void data() {
+//        sendTest();
+        sendTest2();
+    }
+
+//    private void sendTest() {
+//        OkHttpUtil.getInstance(this).post("http://www.yanyi.red/api/AppMsg.php").async(null, new onOkHttpListener() {
+//            @Override
+//            public void onCompleted() {
+//
+//            }
+//
+//            @Override
+//            public void onSuccess(ResultMsg requestMsg) {
+//                JLog.v(requestMsg);
+//            }
+//
+//            @Override
+//            public void onFailure(Throwable t) {
+//
+//            }
+//        });
+//        OkHttpUtil.getInstance(this).post("http://www.yanyi.red/api/TestApi.php").async(null, new onOkHttpListener() {
+//            @Override
+//            public void onCompleted() {
+//                JLog.v();
+//            }
+//
+//            @Override
+//            public void onSuccess(ResultMsg resultMsg) {
+//                JLog.v(resultMsg);
+//            }
+//
+//            @Override
+//            public void onFailure(Throwable throwable) {
+//                throwable.printStackTrace();
+//            }
+//        });
+//    }
+
+    private void sendTest2() {
+        OkHttpUtil.getInstance(this).post("http://www.yanyi.red/api/AppMsg.php").async(null, new onOkHttpListener() {
+            @Override
+            public void onCompleted() {
+                JLog.v();
+            }
+
+            @Override
+            public void onSuccess(ResultMsg resultMsg) {
+                JLog.v(resultMsg);
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+                throwable.printStackTrace();
+            }
+        });
+        OkHttpUtil.getInstance(this).post("http://www.yanyi.red/api/TestApi.php").async(null, new onOkHttpListener() {
+            @Override
+            public void onCompleted() {
+                JLog.v();
+            }
+
+            @Override
+            public void onSuccess(ResultMsg resultMsg) {
+                JLog.v(resultMsg);
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+                throwable.printStackTrace();
+            }
+        });
     }
 
     @Override

+ 19 - 0
okhttp2/src/main/java/com/mylove/okhttp/Cache.java

@@ -0,0 +1,19 @@
+package com.mylove.okhttp;
+
+import android.content.Context;
+
+/**
+ * @author myLove
+ * @time 2017/12/26 17:23
+ * @e-mail love@yanyi.red
+ * @overview
+ */
+
+class Cache {
+    /**
+     * 设置缓存路径,以及缓存文件大小
+     */
+    static okhttp3.Cache privateCache(Context mContext) {
+        return new okhttp3.Cache(mContext.getCacheDir(), 1024 * 1024);
+    }
+}

+ 5 - 5
okhttp2/src/main/java/com/mylove/okhttp/CacheUtils.java

@@ -29,12 +29,12 @@ class CacheUtils {
         if (instance == null) {
             instance = new CacheUtils();
             mContext = context;
-            File dir = mContext.getExternalFilesDir(null);
-            if (dir != null && !dir.exists() && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-                dir.mkdirs();
-            }
-            realFile = dir;
         }
+        File dir = mContext.getExternalFilesDir(null);
+        if (dir != null && !dir.exists() && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+            dir.mkdirs();
+        }
+        realFile = dir;
         return instance;
     }
 

+ 0 - 180
okhttp2/src/main/java/com/mylove/okhttp/DownloadCall.java

@@ -1,180 +0,0 @@
-package com.mylove.okhttp;
-
-import android.annotation.SuppressLint;
-import android.content.Context;
-import android.support.annotation.NonNull;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.concurrent.TimeUnit;
-
-import okhttp3.Cache;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import rx.Subscriber;
-
-/**
- * @author myLove
- * @time 2017/11/24 13:46
- * @e-mail mylove.520.y@gmail.com
- * @overview
- */
-
-class DownloadCall {
-    @SuppressLint("StaticFieldLeak")
-    private static Context mContext;
-    private static String url;
-    private static Subscriber<? super ResultMsg> subscriber;
-    private static Call call;
-    @SuppressLint("StaticFieldLeak")
-    private static DownloadCall instance;
-    private static CallType callType;
-    private static OkHttpClient okHttpClient;
-    private String fileName;
-
-    private DownloadCall() {
-
-    }
-
-    /**
-     * okHttpClient初始化,并添加拦截及缓存
-     *
-     * @param context     上下文
-     * @param str         缓存地址
-     * @param request     请求
-     * @param subscriber1 返回
-     * @param type        请求类型
-     * @return
-     */
-    static DownloadCall getInstance(Context context, String str, Request request, Subscriber<? super ResultMsg> subscriber1, CallType type) {
-        if (instance == null) {
-            synchronized (DownloadCall.class) {
-                if (instance == null) {
-                    mContext = context;
-                    OkHttpClient httpClient = new OkHttpClient();
-                    okHttpClient = httpClient.newBuilder()
-                            .addNetworkInterceptor(new CacheInterceptor())
-                            .cache(privateCache())
-                            .connectTimeout(30, TimeUnit.SECONDS)
-                            .readTimeout(30, TimeUnit.SECONDS)
-                            .build();
-                    instance = new DownloadCall();
-                }
-            }
-        }
-        url = str;
-        subscriber = subscriber1;
-        call = okHttpClient.newCall(request);
-        callType = type;
-        return instance;
-    }
-
-    DownloadCall setFileName(String fileName) {
-        this.fileName = fileName;
-        return this;
-    }
-
-    /**
-     * 请求
-     */
-    void sendCall() {
-        if (callType == CallType.SYNC) {
-            sync();
-        } else if (callType == CallType.ASYNC) {
-            async();
-        }
-    }
-
-    /**
-     * 异步请求
-     */
-    private void sync() {
-        try {
-            Response execute = call.execute();
-            if (execute.isSuccessful()) {
-                save(execute.body());
-            } else {
-                subscriber.onError(new Error("请求失败"));
-            }
-        } catch (IOException e) {
-            subscriber.onError(e);
-        }
-        subscriber.onCompleted();
-    }
-
-    /**
-     * 同步请求
-     */
-    private void async() {
-        call.enqueue(new Callback() {
-            @Override
-            public void onFailure(@NonNull Call call, @NonNull IOException e) {
-                subscriber.onError(e);
-                subscriber.onCompleted();
-            }
-
-            @Override
-            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
-                save(response.body());
-            }
-        });
-    }
-
-    /**
-     * 保存
-     *
-     * @param body
-     */
-    private void save(ResponseBody body) {
-        if (null == fileName && "".equals(fileName) && fileName.length() <= 0) {
-            int lastIndexOf = url.lastIndexOf("/");
-            String pathStr = url.subSequence(lastIndexOf + 1, url.length()).toString();
-            fileName = FileUtil.getSDPath() + "/" + mContext.getPackageName() + "/" + pathStr;
-        }
-        File file = new File(fileName);
-        InputStream is = null;
-        FileOutputStream fos = null;
-        try {
-            byte[] buf = new byte[2048];
-            int len;
-            is = body.byteStream();
-            fos = new FileOutputStream(file);
-            while ((len = is.read(buf)) != -1) {
-                fos.write(buf, 0, len);
-            }
-            fos.flush();
-            ResultMsg msg = new ResultMsg();
-            msg.setCode("200");
-            msg.setResult(fileName);
-            subscriber.onNext(msg);
-        } catch (Exception e) {
-            subscriber.onError(e);
-        } finally {
-            try {
-                if (is != null) {
-                    is.close();
-                }
-                if (fos != null) {
-                    fos.close();
-                }
-            } catch (Exception e) {
-                subscriber.onError(e);
-            }
-        }
-        subscriber.onCompleted();
-    }
-
-    /**
-     * 设置缓存路径,以及缓存文件大小
-     */
-    private static Cache privateCache() {
-        return new Cache(mContext.getCacheDir(), 1024 * 1024);
-    }
-
-}

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

@@ -2,11 +2,22 @@ package com.mylove.okhttp;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
+import android.support.annotation.NonNull;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
+import okhttp3.Call;
+import okhttp3.Callback;
 import okhttp3.FormBody;
+import okhttp3.OkHttpClient;
 import okhttp3.Request;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
 import rx.Observable;
 import rx.Subscriber;
 import rx.android.schedulers.AndroidSchedulers;
@@ -27,6 +38,7 @@ class DownloadObservable {
     private static RequestType requestType;
     private static CallType callType;
     private static String fileName;
+    private static OkHttpClient okHttpClient;
 
     private DownloadObservable() {
     }
@@ -36,13 +48,20 @@ class DownloadObservable {
             synchronized (DownloadObservable.class) {
                 if (instance == null) {
                     instance = new DownloadObservable();
-                    mContext = context;
-                    requestType = type1;
-                    callType = type2;
-                    fileName = file;
+                    OkHttpClient httpClient = new OkHttpClient();
+                    okHttpClient = httpClient.newBuilder()
+                            .addNetworkInterceptor(new CacheInterceptor())
+                            .cache(Cache.privateCache(context))
+                            .connectTimeout(30, TimeUnit.SECONDS)
+                            .readTimeout(30, TimeUnit.SECONDS)
+                            .build();
                 }
             }
         }
+        mContext = context;
+        requestType = type1;
+        callType = type2;
+        fileName = file;
         return instance;
     }
 
@@ -51,6 +70,13 @@ class DownloadObservable {
             synchronized (DownloadObservable.class) {
                 if (instance == null) {
                     instance = new DownloadObservable();
+                    OkHttpClient httpClient = new OkHttpClient();
+                    okHttpClient = httpClient.newBuilder()
+                            .addNetworkInterceptor(new CacheInterceptor())
+                            .cache(Cache.privateCache(context))
+                            .connectTimeout(30, TimeUnit.SECONDS)
+                            .readTimeout(30, TimeUnit.SECONDS)
+                            .build();
                 }
             }
         }
@@ -92,18 +118,104 @@ class DownloadObservable {
     private void send(final String url, final Map<Object, Object> oMap, final Subscriber<? super ResultMsg> subscriber) {
         InternetBean bean = Internet.ifInternet(mContext);
         if (bean.getStatus()) {
-            Request request = getRequest(url, oMap);
-            if (null != fileName && !"".equals(fileName) && fileName.length() > 0) {
-                DownloadCall.getInstance(mContext, url, request, subscriber, callType).setFileName(fileName).sendCall();
-            } else {
-                DownloadCall.getInstance(mContext, url, request, subscriber, callType).sendCall();
-            }
+            Call call = okHttpClient.newCall(getRequest(url, oMap));
+            sendCall(url,call,subscriber);
         } else {
             subscriber.onError(new Exception(bean.getMsg()));
             subscriber.onCompleted();
         }
     }
 
+    /**
+     * 请求
+     */
+    private void sendCall(String url, Call call, Subscriber<? super ResultMsg> subscriber) {
+        if (callType == CallType.SYNC) {
+            sync(url, call, subscriber);
+        } else if (callType == CallType.ASYNC) {
+            async(url, call, subscriber);
+        }
+    }
+
+    /**
+     * 异步请求
+     */
+    private void sync(final String url, Call call, final Subscriber<? super ResultMsg> subscriber) {
+        try {
+            Response execute = call.execute();
+            if (execute.isSuccessful()) {
+                save(execute.body(), url, subscriber);
+            } else {
+                subscriber.onError(new Error("请求失败"));
+            }
+        } catch (IOException e) {
+            subscriber.onError(e);
+        }
+        subscriber.onCompleted();
+    }
+
+    /**
+     * 同步请求
+     */
+    private void async(final String url, Call call, final Subscriber<? super ResultMsg> subscriber) {
+        call.enqueue(new Callback() {
+            @Override
+            public void onFailure(@NonNull Call call, @NonNull IOException e) {
+                subscriber.onError(e);
+                subscriber.onCompleted();
+            }
+
+            @Override
+            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
+                save(response.body(), url, subscriber);
+            }
+        });
+    }
+
+    /**
+     * 保存
+     *
+     * @param body
+     */
+    private void save(ResponseBody body, String url, Subscriber<? super ResultMsg> subscriber) {
+        if (null == fileName && "".equals(fileName) && fileName.length() <= 0) {
+            int lastIndexOf = url.lastIndexOf("/");
+            String pathStr = url.subSequence(lastIndexOf + 1, url.length()).toString();
+            fileName = FileUtil.getSDPath() + "/" + mContext.getPackageName() + "/" + pathStr;
+        }
+        File file = new File(fileName);
+        InputStream is = null;
+        FileOutputStream fos = null;
+        try {
+            byte[] buf = new byte[2048];
+            int len;
+            is = body.byteStream();
+            fos = new FileOutputStream(file);
+            while ((len = is.read(buf)) != -1) {
+                fos.write(buf, 0, len);
+            }
+            fos.flush();
+            ResultMsg msg = new ResultMsg();
+            msg.setCode("200");
+            msg.setResult(fileName);
+            subscriber.onNext(msg);
+        } catch (Exception e) {
+            subscriber.onError(e);
+        } finally {
+            try {
+                if (is != null) {
+                    is.close();
+                }
+                if (fos != null) {
+                    fos.close();
+                }
+            } catch (Exception e) {
+                subscriber.onError(e);
+            }
+        }
+        subscriber.onCompleted();
+    }
+
     /**
      * 判断请求方式
      *

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

@@ -60,7 +60,7 @@ class FileUtil {
     static String getSDPath() {
         File sdDir = null;
         boolean sdCardExist = Environment.getExternalStorageState()
-                .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
+                .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
         if (sdCardExist) {//判断sd卡是否存在
             sdDir = Environment.getExternalStorageDirectory();//获取跟目录
         }

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

@@ -2,15 +2,22 @@ package com.mylove.okhttp;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
+import android.support.annotation.NonNull;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
+import okhttp3.Call;
+import okhttp3.Callback;
 import okhttp3.FormBody;
 import okhttp3.MediaType;
 import okhttp3.MultipartBody;
+import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.RequestBody;
+import okhttp3.Response;
 import rx.Observable;
 import rx.Subscriber;
 import rx.android.schedulers.AndroidSchedulers;
@@ -32,11 +39,21 @@ class ObservableRequest {
     private static RequestType requestType;
     private static CallType callType;
 
+    private static OkHttpClient okHttpClient;
+    private String mCacheUrl = "";
+
     static ObservableRequest getInstance(Context context, RequestType type1, CallType type2) {
         if (instance == null) {
             synchronized (ObservableRequest.class) {
                 if (instance == null) {
                     instance = new ObservableRequest();
+                    OkHttpClient httpClient = new OkHttpClient();
+                    okHttpClient = httpClient.newBuilder()
+                            .addNetworkInterceptor(new CacheInterceptor())
+                            .cache(Cache.privateCache(context))
+                            .connectTimeout(30, TimeUnit.SECONDS)
+                            .readTimeout(30, TimeUnit.SECONDS)
+                            .build();
                 }
             }
         }
@@ -67,7 +84,7 @@ class ObservableRequest {
     }
 
     private Observable<ResultMsg> getObservable(final String url, final Map<Object, Object> oMap) {
-        return Observable.create(new Observable.OnSubscribe<ResultMsg>() {
+        return Observable.unsafeCreate(new Observable.OnSubscribe<ResultMsg>() {
             @Override
             public void call(Subscriber<? super ResultMsg> subscriber) {
                 send(url, oMap, subscriber);
@@ -75,17 +92,18 @@ class ObservableRequest {
         });
     }
 
-    private void send(final String url, final Map<Object, Object> oMap, final Subscriber<? super ResultMsg> subscriber) {
-        final String mCacheUrl;
-        if (FormatUtil.isMapNotEmpty(oMap)) {
-            mCacheUrl = url + oMap.toString();
+    private void send(String url, Map<Object, Object> map, Subscriber<? super ResultMsg> subscriber) {
+//        Call call = okHttpClient.newCall(request(url, map));
+        if (FormatUtil.isMapNotEmpty(map)) {
+            mCacheUrl = url + map.toString();
         } else {
             mCacheUrl = url;
         }
         InternetBean bean = Internet.ifInternet(mContext);
         if (bean.getStatus()) {
-            Request request = getRequest(url, oMap);
-            OkCall.getInstance(mContext, mCacheUrl, request, subscriber, callType).sendCall();
+            Call call = okHttpClient.newCall(getRequest(url, map));
+//            OkCall.getInstance(mContext, mCacheUrl, call, subscriber, callType).sendCall();
+            sendCall(call, subscriber);
         } else {
             String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
             if (FormatUtil.isNotEmpty(json)) {
@@ -98,6 +116,131 @@ class ObservableRequest {
             }
             subscriber.onCompleted();
         }
+
+    }
+
+    /**
+     * 请求
+     */
+    void sendCall(Call call, Subscriber<? super ResultMsg> subscriber) {
+        if (callType == CallType.SYNC) {
+            sync(call, subscriber);
+        } else if (callType == CallType.ASYNC) {
+            async(call, subscriber);
+        }
+    }
+
+    /**
+     * 同步请求
+     */
+    private void sync(Call call, Subscriber<? super ResultMsg> subscriber) {
+        try {
+            Response execute = call.execute();
+            ResultMsg msg = new ResultMsg();
+            int code = execute.code();
+            msg.setCode(code + "");
+            msg.setResult("");
+            if (execute.isSuccessful()) {
+                String str = execute.body().string();
+                msg.setResult(str);
+                if (!str.contains("<!DOCTYPE html>")) {
+                    if (FormatUtil.isNotEmpty(mCacheUrl)) {
+                        CacheUtils.getInstance(mContext).setCacheToLocalJson(mCacheUrl, str);
+                    }
+                }
+                subscriber.onNext(msg);
+                subscriber.onCompleted();
+            } else {
+                String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
+                if (FormatUtil.isNotEmpty(json)) {
+                    msg.setResult(json);
+                    subscriber.onNext(msg);
+                } else {
+                    subscriber.onError(new Exception("请求失败"));
+                }
+                subscriber.onCompleted();
+            }
+        } catch (IOException e) {
+            String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
+            ResultMsg msg = new ResultMsg();
+            msg.setCode("404");
+            if (FormatUtil.isNotEmpty(json)) {
+                msg.setResult(json);
+                subscriber.onNext(msg);
+            } else {
+                subscriber.onError(e);
+            }
+            e.printStackTrace();
+            subscriber.onCompleted();
+        }
+    }
+
+    /**
+     * 异步请求
+     */
+    private void async(Call call, final Subscriber<? super ResultMsg> subscriber) {
+//        call.enqueue(new Callback() {
+//            @Override
+//            public void onFailure(Call call, IOException e) {
+//                subscriber.onError(e);
+//            }
+//
+//            @Override
+//            public void onResponse(Call call, Response response) throws IOException {
+//                String str = response.body().string();
+//                ResultMsg resultMsg = new ResultMsg();
+//                resultMsg.setCode(response.code() + "");
+//                resultMsg.setResult(str);
+//                subscriber.onNext(resultMsg);
+//                subscriber.onCompleted();
+//            }
+//        });
+        call.enqueue(new Callback() {
+            @Override
+            public void onFailure(@NonNull Call call, @NonNull IOException e) {
+                String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
+                ResultMsg msg = new ResultMsg();
+                msg.setCode("404");
+                if (FormatUtil.isNotEmpty(json)) {
+                    msg.setResult(json);
+                    subscriber.onNext(msg);
+                } else {
+                    subscriber.onError(e);
+                }
+                e.printStackTrace();
+                subscriber.onCompleted();
+            }
+
+            @Override
+            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
+                String str = response.body().string();
+                ResultMsg msg = new ResultMsg();
+                int code = response.code();
+                msg.setCode(code + "");
+                msg.setResult(str);
+                if (!str.contains("<!DOCTYPE html>")) {
+                    if (FormatUtil.isNotEmpty(mCacheUrl)) {
+                        CacheUtils.getInstance(mContext).setCacheToLocalJson(mCacheUrl, str);
+                    }
+                }
+                subscriber.onNext(msg);
+                subscriber.onCompleted();
+            }
+        });
+    }
+
+    private Request request(String url, Map<Object, Object> map) {
+        FormBody.Builder builder = new FormBody.Builder();
+        if (FormatUtil.isMapNotEmpty(map)) {
+            for (Map.Entry<Object, Object> entry : map.entrySet()) {
+                builder.add(entry.getKey().toString(), entry.getValue().toString());
+            }
+        }
+        FormBody build = builder.build();
+        return new Request.Builder()
+                .url(url)
+                .post(build)
+                .build();
     }
 
     /**

+ 0 - 174
okhttp2/src/main/java/com/mylove/okhttp/OkCall.java

@@ -1,174 +0,0 @@
-package com.mylove.okhttp;
-
-import android.annotation.SuppressLint;
-import android.content.Context;
-import android.support.annotation.NonNull;
-
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
-
-import okhttp3.Cache;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
-import rx.Subscriber;
-
-/**
- * @author myLove
- * @time 2017/11/15 13:52
- * @e-mail mylove.520.y@gmail.com
- * @overview
- */
-
-class OkCall {
-    @SuppressLint("StaticFieldLeak")
-    private static Context mContext;
-    private static String mCacheUrl;
-    private static Subscriber<? super ResultMsg> subscriber;
-    private static Call call;
-    @SuppressLint("StaticFieldLeak")
-    private static OkCall instance;
-    private static CallType callType;
-    private static OkHttpClient okHttpClient;
-
-    private OkCall() {
-
-    }
-
-    /**
-     * okHttpClient初始化,并添加拦截及缓存
-     *
-     * @param context     上下文
-     * @param cacheUrl    缓存地址
-     * @param request     请求
-     * @param subscriber1 返回
-     * @param type        请求类型
-     * @return
-     */
-    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())
-                            .cache(privateCache())
-                            .connectTimeout(30, TimeUnit.SECONDS)
-                            .readTimeout(30, TimeUnit.SECONDS)
-                            .build();
-                    instance = new OkCall();
-                }
-            }
-        }
-        mCacheUrl = cacheUrl;
-        subscriber = subscriber1;
-        call = okHttpClient.newCall(request);
-        callType = type;
-        return instance;
-    }
-
-
-    /**
-     * 请求
-     */
-    void sendCall() {
-        if (callType == CallType.SYNC) {
-            sync();
-        } else if (callType == CallType.ASYNC) {
-            async();
-        }
-    }
-
-    /**
-     * 同步请求
-     */
-    private void sync() {
-        try {
-            Response execute = call.execute();
-            ResultMsg msg = new ResultMsg();
-            int code = execute.code();
-            msg.setCode(code + "");
-            msg.setResult("");
-            if (execute.isSuccessful()) {
-                String str = execute.body().string();
-                msg.setResult(str);
-                if (!str.contains("<!DOCTYPE html>")) {
-                    if (FormatUtil.isNotEmpty(mCacheUrl)) {
-                        CacheUtils.getInstance(mContext).setCacheToLocalJson(mCacheUrl, str);
-                    }
-                }
-                subscriber.onNext(msg);
-                subscriber.onCompleted();
-            } else {
-                String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
-                if (FormatUtil.isNotEmpty(json)) {
-                    msg.setResult(json);
-                    subscriber.onNext(msg);
-                } else {
-                    subscriber.onError(new Exception("请求失败"));
-                }
-                subscriber.onCompleted();
-            }
-        } catch (IOException e) {
-            String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
-            ResultMsg msg = new ResultMsg();
-            msg.setCode("404");
-            if (FormatUtil.isNotEmpty(json)) {
-                msg.setResult(json);
-                subscriber.onNext(msg);
-            } else {
-                subscriber.onError(e);
-            }
-            e.printStackTrace();
-            subscriber.onCompleted();
-        }
-    }
-
-    /**
-     * 异步请求
-     */
-    private void async() {
-        call.enqueue(new Callback() {
-            @Override
-            public void onFailure(@NonNull Call call, @NonNull IOException e) {
-                String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
-                ResultMsg msg = new ResultMsg();
-                msg.setCode("404");
-                if (FormatUtil.isNotEmpty(json)) {
-                    msg.setResult(json);
-                    subscriber.onNext(msg);
-                } else {
-                    subscriber.onError(e);
-                }
-                e.printStackTrace();
-                subscriber.onCompleted();
-            }
-
-            @Override
-            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
-                String str = response.body().string();
-                ResultMsg msg = new ResultMsg();
-                int code = response.code();
-                msg.setCode(code + "");
-                msg.setResult(str);
-                if (!str.contains("<!DOCTYPE html>")) {
-                    if (FormatUtil.isNotEmpty(mCacheUrl)) {
-                        CacheUtils.getInstance(mContext).setCacheToLocalJson(mCacheUrl, str);
-                    }
-                }
-                subscriber.onNext(msg);
-                subscriber.onCompleted();
-            }
-        });
-    }
-
-    /**
-     * 设置缓存路径,以及缓存文件大小
-     */
-    private static Cache privateCache() {
-        return new Cache(mContext.getCacheDir(), 1024 * 1024);
-    }
-}