yanyi 5 роки тому
батько
коміт
bbcbfefcd5

+ 2 - 1
README.md

@@ -4,9 +4,10 @@
 
 module 下添加
 
-`implementation 'com.yanyi.benyanyi:okhttplib:2.0.1'` 
+`implementation 'com.yanyi.benyanyi:okhttplib:2.0.2'` 
   
 ### 更新记录
+* 2020-01-16(2.0.2) 修复解析崩溃问题,优化返回方法
 * 2019-08-26(2.0.1) 优化下载
 * 2019-08-20(2.0.0) 升级2.x版本,重构代码,优化方法,使调用更简洁明了
 * 2019-07-19(1.1.4) 优化回调方法

+ 2 - 1
app/build.gradle

@@ -34,10 +34,11 @@ dependencies {
     androidTestImplementation 'com.android.support.test:runner:1.0.2'
     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
     implementation project(':okhttplib')
-    implementation 'com.yanyi.benyanyi:loglib:1.0.1'
+    implementation 'com.yanyi.benyanyi:loglib:1.0.3'
     implementation 'com.yanyi.benyanyi:permissionlib:1.0.9'
     implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
     implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
     implementation 'org.greenrobot:eventbus:3.1.1'
+    implementation 'com.google.code.gson:gson:2.8.6'
 
 }

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

@@ -3,7 +3,6 @@ package com.mylove.okhttp;
 import android.app.Application;
 import android.content.Context;
 
-import com.mylove.loglib.JLog;
 
 /**
  * @author myLove
@@ -20,7 +19,6 @@ public class AppContext extends Application {
     @Override
     public void onCreate() {
         super.onCreate();
-        JLog.init(true);
         mContext = this;
         downloadManager = DownloadManager.getInstance(mContext);
     }

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

@@ -6,13 +6,15 @@ import android.support.annotation.Nullable;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 
+import com.benyanyi.loglib.Jlog;
 import com.benyanyi.okhttp.OkHttpUtil;
 import com.benyanyi.okhttp.listener.OnOkHttpListener;
 import com.benyanyi.permissionlib.PermissionCallBack;
 import com.benyanyi.permissionlib.PermissionHelper;
 import com.benyanyi.permissionlib.PermissionType;
 import com.benyanyi.permissionlib.msg.FailureMsg;
-import com.mylove.loglib.JLog;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -47,9 +49,20 @@ public class MainActivity extends AppCompatActivity {
 //
 //            }
 //        });
+        String str = "<html></html>";
+        try {
+//            JSONObject jsonObject = new JSONObject(str);
+            JsonElement jsonParser = JsonParser.parseString(str);
+            Jlog.v(jsonParser.isJsonArray());
+            Jlog.v(jsonParser.isJsonObject());
+            Jlog.v(jsonParser.isJsonPrimitive());
+            Jlog.v(jsonParser.isJsonNull());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 
-    public void onClick(View view){
+    public void onClick(View view) {
         String[] strings = new String[]{PermissionType.STORAGE};
         PermissionHelper.getInstance(this).hasPermission(1, new PermissionCallBack() {
             @Override
@@ -70,7 +83,7 @@ public class MainActivity extends AppCompatActivity {
     }
 
     private void init() {
-        String url = "http://www.yanyi.red/taihao/SimpleAlarm/update.json";
+        String url = "http://qxu1590330342.my3w.com/taihao/SimpleAlarm/update.json";
         Map<Object, Object> oMap = new HashMap<>();
         oMap.put("UserID", "110");
         oMap.put("TypeID", "1");
@@ -86,19 +99,19 @@ public class MainActivity extends AppCompatActivity {
 
             @Override
             public void onSuccess(Object message) {
-                JLog.v(message);
+                Jlog.v(message);
             }
 
             @Override
             public void onFailure(Throwable t) {
-                JLog.e(t.getMessage());
+                Jlog.e(t.getMessage());
             }
         });
     }
 
     private void downloadAPK() {
 //        JLog.init(true);
-        String url = "http://www.yanyi.red/bluetooth/dectector/dectector.apk";
+        String url = "http://qxu1590330342.my3w.com/bluetooth/dectector/dectector.apk";
 //        String filePath = "/dectector/dfu/";
 ////        String filePath = Environment.getExternalStorageDirectory().toString() + "/dectector/dfu/";
 //        OkHttpUtil.getInstance(this).downloadFile(url).downloads(filePath, new OnDownloadCallBack() {

+ 1 - 1
okhttplib/bintrayUpload.gradle

@@ -7,7 +7,7 @@ def siteUrl = 'http://www.yanyis.space/yanyi/baseokhttp' // 项目主页。
 def gitUrl = 'http://www.yanyis.space/yanyi/baseokhttp.git' // Git仓库的url。
 
 group = "com.yanyi.benyanyi"// 唯一包名,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的com.ansen.http就是这里配置的。
-version = "2.0.1" //项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
+version = "2.0.2" //项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
 install {
     repositories.mavenInstaller {
         // This generates POM.xml with proper parameters

+ 27 - 0
okhttplib/proguard-rules.pro

@@ -19,3 +19,30 @@
 # If you keep the line number information, uncomment this to
 # hide the original source file name.
 #-renamesourcefileattribute SourceFile
+#-keep class com.benyanyi.okhttp.*
+#
+#-keep class com.benyanyi.okhttp.listener.*
+#
+#-keepclassmembers class com.benyanyi.okhttp.listener.*{*;}
+#
+#-keepclassmembers class com.benyanyi.okhttp.OkHttpUtil.*{*;}
+#-keepclassmembers class com.benyanyi.okhttp.type.RequestType.*{*;}
+#
+###"$"的含义是保留某类的内部类不会被混淆
+##-keepclassmembers class com.benyanyi.okhttp.TableDao$Builder {
+##     public <methods>;
+##}
+#
+#-keepattributes Signature
+#
+#-keepclassmembers enum * {
+#      public static **[] values();
+#      public static ** valueOf(java.lang.String);
+#}
+##okhttp
+#-dontwarn okhttp3.**
+#-keep class okhttp3.**{*;}
+#
+##okio
+#-dontwarn okio.**
+#-keep class okio.**{*;}

+ 14 - 17
okhttplib/src/main/java/com/benyanyi/okhttp/call/BeanCall.java

@@ -3,12 +3,12 @@ package com.benyanyi.okhttp.call;
 import android.content.Context;
 
 import com.benyanyi.okhttp.config.CallType;
-import com.benyanyi.okhttp.util.OkHttpLog;
+import com.benyanyi.okhttp.listener.OnOkHttpListener;
 import com.benyanyi.okhttp.util.CacheUtils;
 import com.benyanyi.okhttp.util.FormatUtil;
 import com.benyanyi.okhttp.util.Internet;
 import com.benyanyi.okhttp.util.InternetBean;
-import com.benyanyi.okhttp.listener.OnOkHttpListener;
+import com.benyanyi.okhttp.util.OkHttpLog;
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
@@ -138,7 +138,7 @@ class BeanCall<T> {
 
     private void success(Response response, ObservableEmitter<T> emitter) throws IOException {
         ResponseBody body = response.body();
-        if (body != null) {
+        if (body != null && response.code() == 200) {
             if (response.isSuccessful()) {
                 if (response.networkResponse() != null) {//网络请求有数据
                     String str = body.string();
@@ -163,7 +163,7 @@ class BeanCall<T> {
         String str = message;
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (!str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }
@@ -196,6 +196,9 @@ class BeanCall<T> {
     private T dataToT(String data) {
         String str = data;
         OkHttpLog.d(str);
+        if (FormatUtil.isEmpty(str)) {
+            return null;
+        }
         T t = null;
         boolean boo = ("<".equals(str.substring(0, 1)) || "[".equals(str.substring(0, 1)))
                 && ("\"".equals(str.substring(1, 2)) || "[".equals(str.substring(1, 2)));
@@ -206,23 +209,17 @@ class BeanCall<T> {
                 OkHttpLog.e(e.getMessage());
             }
         }
-        JsonParser jsonParser = new JsonParser();
-        JsonElement parse = jsonParser.parse(str);
-        if (parse.isJsonObject()) {
-            try {
+        try {
+            JsonParser jsonParser = new JsonParser();
+            JsonElement parse = jsonParser.parse(str);
+            if (parse.isJsonObject()) {
                 t = new Gson().fromJson(str, tClass);
-            } catch (Exception e) {
-                OkHttpLog.e(e.getMessage());
-            }
-        } else {
-            try {
+            } else {
                 t = new Gson().fromJson(str, tClass);
-            } catch (Exception e) {
-                OkHttpLog.e(e.getMessage());
             }
+        } catch (Exception e) {
+            OkHttpLog.e(e.getMessage());
         }
         return t;
     }
-
-
 }

+ 18 - 23
okhttplib/src/main/java/com/benyanyi/okhttp/call/ListCall.java

@@ -141,7 +141,7 @@ class ListCall<T> {
 
     private void success(Response response, ObservableEmitter<List<T>> emitter) throws IOException {
         ResponseBody body = response.body();
-        if (body != null) {
+        if (body != null && response.code() == 200) {
             if (response.isSuccessful()) {
                 if (response.networkResponse() != null) {//网络请求有数据
                     String str = body.string();
@@ -166,7 +166,7 @@ class ListCall<T> {
         String str = message;
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (!str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }
@@ -198,8 +198,10 @@ class ListCall<T> {
 
     private List<T> dataToT(String data) {
         String str = data;
-        OkHttpLog.d(str);
         List<T> list = new ArrayList<>();
+        if (FormatUtil.isEmpty(str)) {
+            return list;
+        }
         boolean boo = ("<".equals(str.substring(0, 1)) || "[".equals(str.substring(0, 1)))
                 && ("\"".equals(str.substring(1, 2)) || "[".equals(str.substring(1, 2)));
         if (boo) {
@@ -209,29 +211,22 @@ class ListCall<T> {
                 OkHttpLog.e(e.getMessage());
             }
         }
-        JsonParser jsonParser = new JsonParser();
-        JsonElement parse = jsonParser.parse(str);
-        Gson gson = new Gson();
-        if (parse.isJsonArray()) {
-            JsonArray array = parse.getAsJsonArray();
-            for (int i = 0; i < array.size(); i++) {
-                T t = gson.fromJson(array.getAsJsonObject().toString(), tClass);
-                list.add(t);
-            }
-        } else if (parse.isJsonObject()) {
-            try {
-                T t = gson.fromJson(str, tClass);
-                list.add(t);
-            } catch (Exception e) {
-                OkHttpLog.e(e.getMessage());
-            }
-        } else {
-            try {
+        try {
+            JsonParser jsonParser = new JsonParser();
+            JsonElement parse = jsonParser.parse(str);
+            Gson gson = new Gson();
+            if (parse.isJsonArray()) {
+                JsonArray array = parse.getAsJsonArray();
+                for (int i = 0; i < array.size(); i++) {
+                    T t = gson.fromJson(array.getAsJsonObject().toString(), tClass);
+                    list.add(t);
+                }
+            } else {
                 T t = gson.fromJson(str, tClass);
                 list.add(t);
-            } catch (Exception e) {
-                OkHttpLog.e(e.getMessage());
             }
+        } catch (Exception e) {
+            OkHttpLog.e(e.getMessage());
         }
         return list;
     }

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

@@ -130,7 +130,7 @@ class ObjectCall {
 
     private void success(Response response, ObservableEmitter<Object> emitter) throws IOException {
         ResponseBody body = response.body();
-        if (body != null) {
+        if (body != null && response.code() == 200) {
             if (response.isSuccessful()) {
                 if (response.networkResponse() != null) {//网络请求有数据
                     String str = body.string();
@@ -152,9 +152,12 @@ class ObjectCall {
      */
     private String dataProcessing(String message) {
         String str = message;
+        if (FormatUtil.isEmpty(str)) {
+            return null;
+        }
         OkHttpLog.d(str);
         String html = "<!DOCTYPE HTML>";
-        if (!str.toUpperCase().contains(html)) {
+        if (FormatUtil.isNotEmpty(str) && !str.toUpperCase().contains(html)) {
             if (FormatUtil.isNotEmpty(mCacheName)) {
                 CacheUtils.getInstance(context).setCacheToLocalJson(mCacheName, str);
             }

+ 0 - 9
okhttplib/src/main/java/com/benyanyi/okhttp/util/Internet.java

@@ -18,15 +18,6 @@ public class Internet {
                 msg.setStatus(false);
                 msg.setMsg("网络异常");
                 return msg;
-            case 0:
-                if (InternetUtil.isNetWorkConnected(mContext)) {
-                    msg.setStatus(false);
-                    msg.setMsg("网络异常");
-                    return msg;
-                } else {
-                    msg.setStatus(true);
-                    return msg;
-                }
             case 1:
                 if (InternetUtil.isNetWorkConnected(mContext)) {
                     msg.setStatus(false);

+ 2 - 2
okhttplib/src/main/java/com/benyanyi/okhttp/util/OkHttpLog.java

@@ -25,13 +25,13 @@ public class OkHttpLog {
     }
 
     public static void d(Object object) {
-        if (isLOG) {
+        if (isLOG && object != null) {
             Log.d(TAG, object.toString());
         }
     }
 
     public static void e(Object object) {
-        if (isLOG) {
+        if (isLOG && object != null) {
             Log.e(TAG, object.toString());
         }
     }