Quellcode durchsuchen

2.0.6 修复磁盘未挂载情况下报错问题

yanyi vor 4 Jahren
Ursprung
Commit
0dd32840bd

+ 4 - 2
README.md

@@ -17,7 +17,7 @@ repositories {
 ~~~
 ### module 下添加
 ~~~
-implementation 'com.yanyi.benyanyi:okhttplib:2.0.4'
+implementation 'com.yanyi.benyanyi:okhttplib:2.0.6'
 ~~~
 
 或者
@@ -25,12 +25,14 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.4'
 <dependency>
   <groupId>com.yanyi.benyanyi</groupId>
   <artifactId>okhttplib</artifactId>
-  <version>2.0.4</version>
+  <version>2.0.6</version>
   <type>aar</type>
 </dependency>
 ~~~
 
 ### 更新记录
+* 2020-10-24(2.0.6) 修复缓存文件android6.0时磁盘未挂载时报错问题
+* 2020-09-25(2.0.5) 更改超时时间,添加下载文件覆盖
 * 2020-05-16(2.0.4) 修复上传大文件时OOM问题
 * 2020-05-16(2.0.3) 将android support转成androidx支持,并将Maven库存放到个人服务器上
 * 2020-01-16(2.0.2) 修复解析崩溃问题,优化返回方法

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

@@ -6,6 +6,8 @@ import com.benyanyi.okhttp.type.HttpRequest;
 import com.benyanyi.okhttp.type.RequestType;
 import com.benyanyi.okhttp.util.FormatUtil;
 
+import java.io.File;
+
 /**
  * @author YanYi
  * @date 2019-08-02 22:22
@@ -17,6 +19,8 @@ public class OkHttpUtil {
     private static OkHttpUtil instance;
     private static Context mContext;
 
+    public static File cacheFile;
+
     public static OkHttpUtil getInstance(Context context) {
         if (instance == null) {
             instance = new OkHttpUtil();

+ 4 - 10
okhttplib/src/main/java/com/benyanyi/okhttp/call/BeanCall.java

@@ -10,8 +10,6 @@ import com.benyanyi.okhttp.util.Internet;
 import com.benyanyi.okhttp.util.InternetBean;
 import com.benyanyi.okhttp.util.OkHttpLog;
 import com.google.gson.Gson;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
 
 import org.json.XML;
 
@@ -163,7 +161,7 @@ class BeanCall<T> {
         String str = message;
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html) && isCache) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }
@@ -210,13 +208,9 @@ class BeanCall<T> {
             }
         }
         try {
-            JsonParser jsonParser = new JsonParser();
-            JsonElement parse = jsonParser.parse(str);
-            if (parse.isJsonObject()) {
-                t = new Gson().fromJson(str, tClass);
-            } else {
-                t = new Gson().fromJson(str, tClass);
-            }
+//            JsonParser jsonParser = new JsonParser();
+//            JsonElement parse = jsonParser.parse(str);
+            t = new Gson().fromJson(str, tClass);
         } catch (Exception e) {
             OkHttpLog.e(e.getMessage());
         }

+ 4 - 3
okhttplib/src/main/java/com/benyanyi/okhttp/call/ListCall.java

@@ -166,7 +166,7 @@ class ListCall<T> {
         String str = message;
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html) && isCache) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }
@@ -212,8 +212,9 @@ class ListCall<T> {
             }
         }
         try {
-            JsonParser jsonParser = new JsonParser();
-            JsonElement parse = jsonParser.parse(str);
+//            JsonParser jsonParser = new JsonParser();
+//            JsonElement parse = jsonParser.parse(str);
+            JsonElement parse = JsonParser.parseString(str);
             Gson gson = new Gson();
             if (parse.isJsonArray()) {
                 JsonArray array = parse.getAsJsonArray();

+ 2 - 3
okhttplib/src/main/java/com/benyanyi/okhttp/call/ObjectCall.java

@@ -157,7 +157,7 @@ class ObjectCall {
         }
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html) && isCache) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }
@@ -178,10 +178,9 @@ class ObjectCall {
     private void dataReturn(String message, ObservableEmitter<Object> emitter) {
         if (isCache) {
             emitter.onNext(CacheUtils.getInstance(context).getCacheToLocalJson(mCacheName));
-            emitter.onComplete();
         } else {
             emitter.onNext(dataProcessing(message));
-            emitter.onComplete();
         }
+        emitter.onComplete();
     }
 }

+ 16 - 4
okhttplib/src/main/java/com/benyanyi/okhttp/util/CacheUtils.java

@@ -4,6 +4,8 @@ import android.annotation.SuppressLint;
 import android.content.Context;
 import android.os.Environment;
 
+import com.benyanyi.okhttp.OkHttpUtil;
+
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -27,11 +29,21 @@ public final class CacheUtils {
             instance = new CacheUtils();
             mContext = context;
         }
-        File dir = mContext.getExternalFilesDir(null);
-        if (dir != null && !dir.exists() && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-            dir.mkdirs();
+        if (OkHttpUtil.cacheFile == null) {
+            File dir = mContext.getExternalFilesDir(null);
+            if (dir != null && !dir.exists() && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+                dir.mkdirs();
+            } else {
+                dir = mContext.getFilesDir();
+                if (!dir.exists()) {
+                    dir.mkdirs();
+                }
+            }
+            realFile = dir;
+        } else {
+            realFile = OkHttpUtil.cacheFile;
         }
-        realFile = dir;
+
         return instance;
     }