浏览代码

更新文档

drake 4 年之前
父节点
当前提交
391f865f9f

+ 1 - 1
README.md

@@ -112,7 +112,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
 // 支持自动下拉刷新和缺省页的(可选)
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.13'
 implementation 'com.github.liangjingkanji:BRV:1.3.13'
 
 
-implementation 'com.github.liangjingkanji:Net:2.3.3'
+implementation 'com.github.liangjingkanji:Net:2.3.4'
 ```
 ```
 
 
 <br>
 <br>

+ 23 - 4
docs/convert.md

@@ -15,17 +15,31 @@
 
 
 === "Gson"
 === "Gson"
     ```kotlin
     ```kotlin
-    class JsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+    class GsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+        val gson = GsonBuilder().serializeNulls().create()
+
         override fun <S> String.parseBody(succeed: Type): S? {
         override fun <S> String.parseBody(succeed: Type): S? {
-            return Gson().fromJson(this, succeed)
+            return gson.fromJson(this, succeed)
         }
         }
     }
     }
     ```
     ```
 === "Moshi"
 === "Moshi"
     ```kotlin
     ```kotlin
-    class JsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+    class MoshiConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+        val moshi = Moshi.Builder().build()
+
         override fun <S> String.parseBody(succeed: Type): S? {
         override fun <S> String.parseBody(succeed: Type): S? {
-            return Moshi.Builder().build().adapter<S>(succeed).fromJson(this)
+
+            return moshi.adapter<S>(succeed).fromJson(this)
+        }
+    }
+    ```
+=== "FastJson"
+    ```kotlin
+    class FastJsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+
+        override fun <S> String.parseBody(succeed: Type): S? {
+            return JSON.parseObject(this, succeed)
         }
         }
     }
     }
     ```
     ```
@@ -35,6 +49,11 @@
 
 
 <br>
 <br>
 
 
+!!! note
+    注意解析器(Gson或者Moshi)的对象记得定义为类成员, 这样可以不会导致每次解析都要创建一个新的对象, 减少内存消耗
+
+<br>
+
 ## 设置转换器
 ## 设置转换器
 转换器分为全局和单例, 单例可以覆盖全局的转换器, 一般情况下设置一个全局即可
 转换器分为全局和单例, 单例可以覆盖全局的转换器, 一般情况下设置一个全局即可
 
 

+ 20 - 5
docs/log-recorder.md

@@ -4,11 +4,20 @@ Net扩展`Okhttp Profiler`插件支持更好的日志拦截信息, 支持加密
 
 
 ## 安装插件
 ## 安装插件
 
 
+
+### 1) 安装插件
 在插件市场搜索: "`Okhttp Profiler`"
 在插件市场搜索: "`Okhttp Profiler`"
 
 
 <img src="https://i.imgur.com/Pvncs1W.png" width="100%"/>
 <img src="https://i.imgur.com/Pvncs1W.png" width="100%"/>
 
 
-### 初始化
+
+### 2) 打开窗口
+安装以后在AndroidStudio右下角打开窗口
+
+<img src="https://i.imgur.com/lZ0RvN4.png" width="80%"/>
+
+
+### 3) 初始化
 ```kotlin hl_lines="3"
 ```kotlin hl_lines="3"
 initNet("http://182.92.97.186/") {
 initNet("http://182.92.97.186/") {
     converter(JsonConvert()) // 转换器
     converter(JsonConvert()) // 转换器
@@ -28,7 +37,7 @@ initNet("http://182.92.97.186/") {
 | <img src="https://i.imgur.com/WG2WgBy.png" width="20%"/> 清空 | 清空记录 |
 | <img src="https://i.imgur.com/WG2WgBy.png" width="20%"/> 清空 | 清空记录 |
 
 
 
 
-### 自定义转换器
+### 响应字符串解密
 
 
 假设你使用的是`DefaultConvert`或者没有使用转换器直接返回String则无需多余处理, 如果覆写或者直接实现的`Convert`, 请确保`result.logResponseBody`被赋值
 假设你使用的是`DefaultConvert`或者没有使用转换器直接返回String则无需多余处理, 如果覆写或者直接实现的`Convert`, 请确保`result.logResponseBody`被赋值
 ```kotlin hl_lines="16"
 ```kotlin hl_lines="16"
@@ -82,9 +91,15 @@ class NetInterceptor : Interceptor {
 | request.log | 请求的日志信息, 默认是params |
 | request.log | 请求的日志信息, 默认是params |
 | response.log | 响应的日志信息, 默认为空 |
 | response.log | 响应的日志信息, 默认为空 |
 
 
-!!! note
-    实际上Net的网络日志还是会被打印到LogCat, 然后通过插件捕捉显示. 如果不想LogCat的冗余日志影响查看其它日志, 可以通过AndroidStudio的功能折叠隐藏
-    <img src="https://i.imgur.com/F6DoICr.png" width="100%"/>
+
+
+## LogCat冗余日志过滤
+实际上Net的网络日志还是会被打印到LogCat, 然后通过插件捕捉显示.
+
+<img src="https://i.imgur.com/0BZAg4M.png" width="50%"/>
+
+如果不想LogCat的冗余日志影响查看其它日志, 可以通过AndroidStudio的功能折叠隐藏, 添加一个`OKPREL_`过滤字段即可
+<img src="https://i.imgur.com/F6DoICr.png" width="100%"/>
 
 
 
 
 ## 扩展至其他请求框架
 ## 扩展至其他请求框架

+ 1 - 0
sample/build.gradle

@@ -80,6 +80,7 @@ dependencies {
     implementation "com.squareup.moshi:moshi-kotlin:1.8.0"
     implementation "com.squareup.moshi:moshi-kotlin:1.8.0"
     kapt "com.squareup.moshi:moshi-kotlin-codegen:1.8.0"
     kapt "com.squareup.moshi:moshi-kotlin-codegen:1.8.0"
     implementation 'com.google.code.gson:gson:2.8.6'
     implementation 'com.google.code.gson:gson:2.8.6'
+    implementation 'com.alibaba:fastjson:1.2.48'
 
 
     // ------------------------------我的其他库-------------------------------------
     // ------------------------------我的其他库-------------------------------------
     implementation 'com.github.liangjingkanji:StatusBar:1.0.3' // 透明状态栏
     implementation 'com.github.liangjingkanji:StatusBar:1.0.3' // 透明状态栏

+ 2 - 2
sample/src/main/java/com/drake/net/sample/base/App.kt

@@ -21,7 +21,7 @@ import com.drake.net.cacheEnabled
 import com.drake.net.initNet
 import com.drake.net.initNet
 import com.drake.net.sample.BR
 import com.drake.net.sample.BR
 import com.drake.net.sample.R
 import com.drake.net.sample.R
-import com.drake.net.sample.callback.JsonConvert
+import com.drake.net.sample.callback.MoshiConvert
 import com.drake.statelayout.StateConfig
 import com.drake.statelayout.StateConfig
 import com.scwang.smart.refresh.footer.ClassicsFooter
 import com.scwang.smart.refresh.footer.ClassicsFooter
 import com.scwang.smart.refresh.header.MaterialHeader
 import com.scwang.smart.refresh.header.MaterialHeader
@@ -40,7 +40,7 @@ class App : Application() {
         }
         }
 
 
         initNet("http://182.92.97.186/") {
         initNet("http://182.92.97.186/") {
-            converter(JsonConvert()) // 自动解析JSON映射到实体类中, 转换器分为全局和单例, 覆盖生效(拦截器允许多个)
+            converter(MoshiConvert()) // 自动解析JSON映射到实体类中, 转换器分为全局和单例, 覆盖生效(拦截器允许多个)
             cacheEnabled()
             cacheEnabled()
             setLogRecord(true)
             setLogRecord(true)
         }
         }

+ 29 - 0
sample/src/main/java/com/drake/net/sample/callback/FastJsonConvert.kt

@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2018 Drake, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.drake.net.sample.callback
+
+import com.alibaba.fastjson.JSON
+import com.drake.net.convert.DefaultConvert
+import java.lang.reflect.Type
+
+class FastJsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+
+    override fun <S> String.parseBody(succeed: Type): S? {
+
+        return JSON.parseObject(this, succeed)
+    }
+}

+ 2 - 1
sample/src/main/java/com/drake/net/sample/callback/GsonConvert.kt

@@ -21,8 +21,9 @@ import com.google.gson.GsonBuilder
 import java.lang.reflect.Type
 import java.lang.reflect.Type
 
 
 class GsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
 class GsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+    val gson = GsonBuilder().serializeNulls().create()
 
 
     override fun <S> String.parseBody(succeed: Type): S? {
     override fun <S> String.parseBody(succeed: Type): S? {
-        return GsonBuilder().serializeNulls().create().fromJson(this, succeed)
+        return gson.fromJson(this, succeed)
     }
     }
 }
 }

+ 4 - 2
sample/src/main/java/com/drake/net/sample/callback/JsonConvert.kt → sample/src/main/java/com/drake/net/sample/callback/MoshiConvert.kt

@@ -20,9 +20,11 @@ import com.drake.net.convert.DefaultConvert
 import com.squareup.moshi.Moshi
 import com.squareup.moshi.Moshi
 import java.lang.reflect.Type
 import java.lang.reflect.Type
 
 
-class JsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+class MoshiConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+    val moshi = Moshi.Builder().build()
 
 
     override fun <S> String.parseBody(succeed: Type): S? {
     override fun <S> String.parseBody(succeed: Type): S? {
-        return Moshi.Builder().build().adapter<S>(succeed).fromJson(this)
+
+        return moshi.adapter<S>(succeed).fromJson(this)
     }
     }
 }
 }