refactor: 重构
This commit is contained in:
1
lib_net/.gitignore
vendored
Normal file
1
lib_net/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/build
|
||||
1
lib_net/build.gradle.kts
Normal file
1
lib_net/build.gradle.kts
Normal file
@ -0,0 +1 @@
|
||||
apply("../buildGradleScript/lib_net.gradle")
|
||||
0
lib_net/consumer-rules.pro
Normal file
0
lib_net/consumer-rules.pro
Normal file
21
lib_net/proguard-rules.pro
vendored
Normal file
21
lib_net/proguard-rules.pro
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
8
lib_net/src/main/AndroidManifest.xml
Normal file
8
lib_net/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.quyunshuo.androidbaseframemvvm.net">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
</manifest>
|
||||
@ -0,0 +1,47 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.net
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import com.google.auto.service.AutoService
|
||||
import com.quyunshuo.androidbaseframemvvm.base.app.ApplicationLifecycle
|
||||
import com.quyunshuo.androidbaseframemvvm.base.app.InitDepend
|
||||
|
||||
/**
|
||||
* 网络模块的Application
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/23/21 6:14 PM
|
||||
*/
|
||||
@AutoService(ApplicationLifecycle::class)
|
||||
class NetApplication : ApplicationLifecycle {
|
||||
|
||||
/**
|
||||
* 同[Application.attachBaseContext]
|
||||
* @param context Context
|
||||
*/
|
||||
override fun onAttachBaseContext(context: Context) {}
|
||||
|
||||
/**
|
||||
* 同[Application.onCreate]
|
||||
* @param application Application
|
||||
*/
|
||||
override fun onCreate(application: Application) {}
|
||||
|
||||
/**
|
||||
* 同[Application.onTerminate]
|
||||
* @param application Application
|
||||
*/
|
||||
override fun onTerminate(application: Application) {}
|
||||
|
||||
/**
|
||||
* 需要立即进行初始化的放在这里进行并行初始化
|
||||
* 需要必须在主线程初始化的放在[InitDepend.mainThreadDepends],反之放在[InitDepend.workerThreadDepends]
|
||||
* @return InitDepend 初始化方法集合
|
||||
*/
|
||||
override fun initByFrontDesk(): InitDepend = InitDepend(mutableListOf(), mutableListOf())
|
||||
|
||||
/**
|
||||
* 不需要立即初始化的放在这里进行后台初始化
|
||||
*/
|
||||
override fun initByBackstage() {}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.net
|
||||
|
||||
/**
|
||||
* 接口公共地址
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/17/21 3:27 PM
|
||||
*/
|
||||
internal object NetBaseUrl {
|
||||
|
||||
const val BASE_URLl = ""
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.net
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* 网络客户端提供者
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/17/21 2:35 PM
|
||||
*/
|
||||
object NetClientProvider {
|
||||
|
||||
/**
|
||||
* 连接超时
|
||||
*/
|
||||
private const val CONNECT_TIME_OUT = 15L * 1000L
|
||||
|
||||
/**
|
||||
* 读取超时
|
||||
*/
|
||||
private const val READ_TIME_OUT = 20L * 1000L
|
||||
|
||||
/**
|
||||
* 日志拦截器
|
||||
*/
|
||||
private val mLogInterceptor: Interceptor by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
val level = if (BuildConfig.DEBUG) {
|
||||
HttpLoggingInterceptor.Level.BODY
|
||||
} else {
|
||||
HttpLoggingInterceptor.Level.NONE
|
||||
}
|
||||
HttpLoggingInterceptor().setLevel(level)
|
||||
}
|
||||
|
||||
/**
|
||||
* OkHttpClient
|
||||
*/
|
||||
private val mOkHttpClient: OkHttpClient by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS) // 连接超时
|
||||
.readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS) // 读取超时
|
||||
.addInterceptor(mLogInterceptor) // 日志拦截器
|
||||
.retryOnConnectionFailure(true) // 失败重连
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目服务器Retrofit
|
||||
*/
|
||||
val mRetrofit: Retrofit by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(NetBaseUrl.BASE_URLl)
|
||||
.addConverterFactory(GsonConverterFactory.create()) // Gson转换器
|
||||
.client(mOkHttpClient)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user