Преглед на файлове

Fix download did not return a problem.

yanyi преди 6 години
родител
ревизия
2152d77a3e

+ 1 - 2
okhttplib/src/main/java/com/mylove/okhttp/Cache.java

@@ -2,7 +2,6 @@ package com.mylove.okhttp;
 
 import android.content.Context;
 import android.support.annotation.NonNull;
-import android.util.Log;
 
 import okhttp3.logging.HttpLoggingInterceptor;
 
@@ -23,7 +22,7 @@ class Cache {
                 @Override
                 public void log(@NonNull String message) {
                     if (OkHttpInfo.isLOG) {
-                        Log.v(OkHttpInfo.TAG, message);
+                        LogHelper.d( message);
                     }
                 }
             }).setLevel(HttpLoggingInterceptor.Level.BODY);

+ 22 - 0
okhttplib/src/main/java/com/mylove/okhttp/DownloadBean.java

@@ -0,0 +1,22 @@
+package com.mylove.okhttp;
+
+/**
+ * @author BenYanYi
+ * @date 2018/9/12 15:32
+ * @email ben@yanyi.red
+ * @overview 下载状态
+ */
+class DownloadBean {
+    public int progress;
+    public int status = 0;
+    public String filePath;
+
+    @Override
+    public String toString() {
+        return "DownloadBean{" +
+                "progress=" + progress +
+                ", status=" + status +
+                ", filePath='" + filePath + '\'' +
+                '}';
+    }
+}

+ 45 - 21
okhttplib/src/main/java/com/mylove/okhttp/DownloadObservable.java

@@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
 import android.content.Context;
 import android.os.Environment;
 import android.support.annotation.NonNull;
-import android.util.Log;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -62,7 +61,9 @@ class DownloadObservable {
 
     void request(String url, String filePath, final OnDownloadListener onDownloadListener) {
         this.filePath = filePath;
-        getObservable(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
+        getObservable(url)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
                 .subscribe(new Observer<DownloadBean>() {
                     @Override
                     public void onSubscribe(Disposable d) {
@@ -71,6 +72,9 @@ class DownloadObservable {
 
                     @Override
                     public void onNext(DownloadBean bean) {
+                        if (OkHttpInfo.isLOG) {
+                            LogHelper.v(bean);
+                        }
                         onDownloadListener.onDownloading(bean.progress);
                         if (bean.status == 1) {
                             onDownloadListener.onSuccess(bean.filePath);
@@ -79,11 +83,15 @@ class DownloadObservable {
 
                     @Override
                     public void onError(Throwable e) {
+                        if (OkHttpInfo.isLOG)
+                            LogHelper.e(e.getMessage());
                         onDownloadListener.onFailure(e);
                     }
 
                     @Override
                     public void onComplete() {
+                        if (OkHttpInfo.isLOG)
+                            LogHelper.v("*****");
                         onDownloadListener.onCompleted();
                     }
                 });
@@ -98,7 +106,7 @@ class DownloadObservable {
         });
     }
 
-    private void send(final String url, final ObservableEmitter<DownloadBean> subscriber) {
+    private void send(final String url, ObservableEmitter<DownloadBean> subscriber) {
         InternetBean bean = Internet.ifInternet(mContext);
         if (bean.getStatus()) {
             Request request = new Request.Builder()
@@ -106,20 +114,42 @@ class DownloadObservable {
                     .build();
             Call call = okHttpClient.newCall(request);
             sendCall(url, call, subscriber);
+//            sendCall(url, call, new onOkHttpListener() {
+//                @Override
+//                public void onCompleted() {
+//                    subscriber.onComplete();
+//                }
+//
+//                @Override
+//                public <T> void onSuccess(T message) {
+//                    DownloadBean bean1 = (DownloadBean) message;
+//                    LogHelper.v(bean1.toString());
+//                    subscriber.onNext(bean1);
+//                }
+//
+//                @Override
+//                public void onFailure(Throwable t) {
+//                    LogHelper.e(t.getMessage());
+//                    subscriber.onError(t);
+//                }
+//            });
         } else {
             subscriber.onError(new Exception(bean.getMsg()));
         }
-        subscriber.onComplete();
     }
 
     /**
      * 请求
      */
     private void sendCall(final String url, Call call, final ObservableEmitter<DownloadBean> subscriber) {
+
         call.enqueue(new Callback() {
             @Override
             public void onFailure(@NonNull Call call, @NonNull IOException e) {
+                if (OkHttpInfo.isLOG)
+                    LogHelper.e(e.getMessage());
                 subscriber.onError(e);
+                subscriber.onComplete();
             }
 
             @Override
@@ -131,13 +161,13 @@ class DownloadObservable {
                 FileOutputStream fos = null;
                 // 储存下载文件的目录
                 String savePath = isExistDir(filePath);
-                if (OkHttpInfo.isLOG)
-                    Log.d("路径", filePath);
                 try {
                     is = response.body().byteStream();
                     long total = response.body().contentLength();
                     File file = new File(savePath, getNameFromUrl(url));
                     bean.filePath = file.getAbsolutePath();
+                    if (OkHttpInfo.isLOG)
+                        LogHelper.d(filePath);
                     fos = new FileOutputStream(file);
                     long sum = 0;
                     while ((len = is.read(buf)) != -1) {
@@ -145,38 +175,41 @@ class DownloadObservable {
                         sum += len;
                         int progress = (int) (sum * 1.0f / total * 100);
                         if (OkHttpInfo.isLOG)
-                            Log.d("进度", progress + "%");
+                            LogHelper.i(progress + "%");
                         bean.status = 0;
                         bean.progress = progress;
+                        if (OkHttpInfo.isLOG)
+                            LogHelper.d(bean);
                         // 下载中
                         subscriber.onNext(bean);
                     }
                     fos.flush();
                     // 下载完成
                     bean.status = 1;
+                    if (OkHttpInfo.isLOG)
+                        LogHelper.d(bean);
                     subscriber.onNext(bean);
                 } catch (Exception e) {
                     if (OkHttpInfo.isLOG)
-                        Log.e("报错", e.getMessage());
+                        LogHelper.e(e.getMessage());
                     subscriber.onError(e);
                 } finally {
                     try {
                         if (is != null)
                             is.close();
                     } catch (IOException e) {
-                        e.printStackTrace();
                         if (OkHttpInfo.isLOG)
-                            Log.e("报错", e.getMessage());
+                            LogHelper.e(e.getMessage());
                     }
                     try {
                         if (fos != null)
                             fos.close();
                     } catch (IOException e) {
-                        e.printStackTrace();
                         if (OkHttpInfo.isLOG)
-                            Log.e("报错", e.getMessage());
+                            LogHelper.e(e.getMessage());
                     }
                 }
+                subscriber.onComplete();
             }
         });
     }
@@ -203,13 +236,4 @@ class DownloadObservable {
     private String getNameFromUrl(String url) {
         return url.substring(url.lastIndexOf("/") + 1);
     }
-
-    /**
-     * 下载状态
-     */
-    class DownloadBean {
-        int progress;
-        int status = 0;
-        String filePath;
-    }
 }

+ 37 - 0
okhttplib/src/main/java/com/mylove/okhttp/LogHelper.java

@@ -0,0 +1,37 @@
+package com.mylove.okhttp;
+
+import android.util.Log;
+
+
+/**
+ * @author BenYanYi
+ * @date 2018/9/12 15:27
+ * @email ben@yanyi.red
+ * @overview 打印日志
+ */
+public class LogHelper {
+
+    public static void a(Object object) {
+        Log.wtf(OkHttpInfo.TAG, object.toString());
+    }
+
+    public static void e(Object object) {
+        Log.e(OkHttpInfo.TAG, object.toString());
+    }
+
+    public static void d(Object object) {
+        Log.d(OkHttpInfo.TAG, object.toString());
+    }
+
+    public static void i(Object object) {
+        Log.i(OkHttpInfo.TAG, object.toString());
+    }
+
+    public static void v(Object object) {
+        Log.v(OkHttpInfo.TAG, object.toString());
+    }
+
+    public static void w(Object object) {
+        Log.w(OkHttpInfo.TAG, object.toString());
+    }
+}

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

@@ -150,7 +150,6 @@ class ObservableRequest {
                     }
                 }
                 subscriber.onNext(str);
-                subscriber.onComplete();
             } else {
                 String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
                 if (FormatUtil.isNotEmpty(json)) {
@@ -158,7 +157,6 @@ class ObservableRequest {
                 } else {
                     subscriber.onError(new Exception("请求失败"));
                 }
-                subscriber.onComplete();
             }
         } catch (IOException e) {
             String json = CacheUtils.getInstance(mContext).getCacheToLocalJson(mCacheUrl);
@@ -168,8 +166,8 @@ class ObservableRequest {
                 subscriber.onError(e);
             }
             e.printStackTrace();
-            subscriber.onComplete();
         }
+        subscriber.onComplete();
     }
 
     /**

+ 4 - 2
okhttplib/src/main/java/com/mylove/okhttp/ObservableRequests.java

@@ -122,8 +122,8 @@ class ObservableRequests<T> {
             } else {
                 subscriber.onError(new Error(bean.getMsg()));
             }
+            subscriber.onComplete();
         }
-        subscriber.onComplete();
     }
 
     /**
@@ -182,7 +182,7 @@ class ObservableRequests<T> {
             }
             e.printStackTrace();
         }
-
+        subscriber.onComplete();
     }
 
     /**
@@ -200,6 +200,7 @@ class ObservableRequests<T> {
                     subscriber.onError(e);
                 }
                 e.printStackTrace();
+                subscriber.onComplete();
             }
 
             @Override
@@ -223,6 +224,7 @@ class ObservableRequests<T> {
                 }
                 T t = new Gson().fromJson(str, tClass);
                 subscriber.onNext(t);
+                subscriber.onComplete();
             }
         });
     }