Modify: 网络请求

This commit is contained in:
Quyunshuo
2020-08-29 11:28:13 +08:00
parent 2e4c78cdd9
commit 0d68e2a7c9
12 changed files with 211 additions and 4 deletions

View File

@ -0,0 +1,3 @@
package com.quyunshuo.common.bean
data class TestBean(val msgTest: String)

View File

@ -0,0 +1,11 @@
package com.quyunshuo.common.constant
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: NetUrl
* @Remark: 请求公共地址 统一放在此类
*/
object NetUrl {
const val url1 = "https://api.com/"
}

View File

@ -0,0 +1,11 @@
package com.quyunshuo.common.constant
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: SpKey
* @Remark: 本地存储的键 放在此类中
*/
object SpKey {
const val IS_FIRST_START = "is_first_start"
}

View File

@ -0,0 +1,23 @@
package com.quyunshuo.common.net
import com.quyunshuo.common.net.api.ApiCommonService
import com.quyunshuo.common.net.api.ApiHomeService
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: NetRequest
* @Remark: 对ApiService动态代理对象统一管理
*/
object NetRequest {
// Home Service
val homeService by lazy(mode = LazyThreadSafetyMode.NONE) {
NetServiceCreator.create(ApiHomeService::class.java)
}
// 公共接口
val commonService by lazy(mode = LazyThreadSafetyMode.NONE) {
NetServiceCreator.create(ApiCommonService::class.java)
}
}

View File

@ -0,0 +1,61 @@
package com.quyunshuo.common.net
import com.quyunshuo.base.BuildConfig
import com.quyunshuo.common.constant.NetUrl
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: NetServiceCreator
* @Remark: Retrofit动态代理对象获取封装
*/
object NetServiceCreator {
private const val BASE_URL = NetUrl.url1
private const val CONNECT_TIME_OUT = 15L
private const val READ_TIME_OUT = 20L
private val BODY by lazy(mode = LazyThreadSafetyMode.NONE) {
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}
private val NONE by lazy(mode = LazyThreadSafetyMode.NONE) {
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.NONE)
}
private val okHttpClient by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
OkHttpClient.Builder()
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS) // 连接超时
.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS) // 读取超时
.addInterceptor(if (BuildConfig.DEBUG) BODY else NONE) // 请求日志拦截器
.retryOnConnectionFailure(true) // 失败重连
.build()
}
private val retrofit by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) // Gson转换器
.client(okHttpClient)
.build()
}
/**
* 获取service动态代理对象
* @param serviceClass 接口Class对象
*/
fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)
/**
* 获取service动态代理对象
* 范型实化
*/
inline fun <reified T> create(): T = create(T::class.java)
}

View File

@ -0,0 +1,9 @@
package com.quyunshuo.common.net.api
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: ApiCommonService
* @Remark: 公共接口
*/
interface ApiCommonService

View File

@ -0,0 +1,19 @@
package com.quyunshuo.common.net.api
import com.quyunshuo.common.bean.TestBean
import retrofit2.http.GET
import retrofit2.http.Query
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: ApiService
* @Remark: Home接口
*/
interface ApiHomeService {
// Retrofit 2.6版本开始支持协程 只需要将抽象方法写成挂起函数即可
@GET()
suspend fun getTestData(@Query("test") test: String): TestBean
}