drake пре 3 година
родитељ
комит
6a429d92cf
2 измењених фајлова са 35 додато и 2 уклоњено
  1. 28 2
      docs/config.md
  2. 7 0
      sample/src/main/java/com/drake/net/sample/base/App.kt

+ 28 - 2
docs/config.md

@@ -1,5 +1,7 @@
 全局配置建议在Application的onCreate函数中配置
 全局配置建议在Application的onCreate函数中配置
 
 
+## 初始配置
+
 === "普通初始化"
 === "普通初始化"
 
 
     ```kotlin
     ```kotlin
@@ -10,6 +12,12 @@
             // http://google.com/  这是接口全局域名, 可以使用NetConfig.host进行单独的修改
             // http://google.com/  这是接口全局域名, 可以使用NetConfig.host进行单独的修改
 
 
             NetConfig.init("http://github.com/") {
             NetConfig.init("http://github.com/") {
+
+                // 超时设置
+                connectTimeout(2, TimeUnit.MINUTES)
+                readTimeout(2, TimeUnit.MINUTES)
+                writeTimeout(2, TimeUnit.MINUTES
+
                 setLog(BuildConfig.DEBUG) // 作用域发生异常是否打印
                 setLog(BuildConfig.DEBUG) // 作用域发生异常是否打印
                 setConverter(GsonConvert()) // 转换器
                 setConverter(GsonConvert()) // 转换器
             }
             }
@@ -35,7 +43,7 @@
     }
     }
     ```
     ```
 
 
-> 配置都是可选项, 不是不初始化就不能使用
+> 配置都是可选项, 不是不初始化就不能使用. 在Xposed项目中可能需要使用 `NetConfig.app = this`
 
 
 在initNet函数作用域中的this属于`OkHttpClient.Builder()`, 可以配置任何OkHttp参数选项
 在initNet函数作用域中的this属于`OkHttpClient.Builder()`, 可以配置任何OkHttp参数选项
 
 
@@ -48,11 +56,29 @@
 | setErrorHandler | [全局错误处理](error-handle.md) |
 | setErrorHandler | [全局错误处理](error-handle.md) |
 | setDialogFactory | [全局对话框](auto-dialog.md) |
 | setDialogFactory | [全局对话框](auto-dialog.md) |
 
 
+## 重试次数
+
+默认情况下你设置超时时间即可, OkHttp内部也有重试机制.
+
+但是个别开发者需求指定重试次数则可以添加`RetryInterceptor`拦截器即可实现失败以后会重试指定次数
+
+```kotlin
+NetConfig.init("http://github.com/") {
+    // ... 其他配置
+    addInterceptor(RetryInterceptor(3)) // 如果全部失败会重试三次
+}
+```
+
+
 ## 动态配置
 ## 动态配置
 
 
 单例[NetConfig](api/-net/com.drake.net/-net-config/index.html)存储了初始化时的配置, 可以后期动态读写.
 单例[NetConfig](api/-net/com.drake.net/-net-config/index.html)存储了初始化时的配置, 可以后期动态读写.
 
 
-例如Retrofit的动态`BaseURL`功能就可以直接修改`NetConfig.host`
+例如Retrofit的动态`baseURL`功能就可以直接修改`NetConfig.host`
+
+```kotlin
+NetConfig.host = "https://github.com/"
+```
 
 
 <img src="https://i.imgur.com/gOhMDUZ.png" width="480"/>
 <img src="https://i.imgur.com/gOhMDUZ.png" width="480"/>
 
 

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

@@ -33,6 +33,7 @@ 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
 import com.scwang.smart.refresh.layout.SmartRefreshLayout
 import com.scwang.smart.refresh.layout.SmartRefreshLayout
+import java.util.concurrent.TimeUnit
 
 
 class App : Application() {
 class App : Application() {
 
 
@@ -40,6 +41,12 @@ class App : Application() {
         super.onCreate()
         super.onCreate()
 
 
         NetConfig.init("http://43.128.31.195/") {
         NetConfig.init("http://43.128.31.195/") {
+
+            // 超时设置
+            connectTimeout(2, TimeUnit.MINUTES)
+            readTimeout(2, TimeUnit.MINUTES)
+            writeTimeout(2, TimeUnit.MINUTES)
+
             setLog(BuildConfig.DEBUG) // LogCat异常日志
             setLog(BuildConfig.DEBUG) // LogCat异常日志
             addInterceptor(LogRecordInterceptor(BuildConfig.DEBUG)) // 添加日志记录器
             addInterceptor(LogRecordInterceptor(BuildConfig.DEBUG)) // 添加日志记录器
             setRequestInterceptor(object : RequestInterceptor { // 添加请求拦截器
             setRequestInterceptor(object : RequestInterceptor { // 添加请求拦截器