Skip to content

Commit

Permalink
BaseRetrofitClient 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenBen committed Aug 7, 2023
1 parent 84fd3c8 commit ff1b310
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.shencoder'
artifactId = 'mvvmkit'
version = '1.1.3'
version = '1.1.4'
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions lib/src/main/java/com/shencoder/mvvmkit/http/BaseRetrofitClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@ abstract class BaseRetrofitClient {

abstract fun generateOkHttpBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder

open fun generateRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder{
open fun generateRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder {
return builder
}

fun <T> getApiService(service: Class<T>, baseUrl: String): T {
//默认添加对Moshi的支持
val moshi = Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()

@JvmOverloads
fun <T> getApiService(
service: Class<T>,
baseUrl: String,
useDefaultMoshiConverterFactory: Boolean = true
): T {
val retrofitBuilder = Retrofit.Builder()
.baseUrl(baseUrl)
.client(generateOkHttpBuilder(OkHttpClient.Builder()).build())
.addConverterFactory(MoshiConverterFactory.create(moshi))
.also {
if (useDefaultMoshiConverterFactory) {
//默认添加对Moshi的支持
val moshi = Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()
it.addConverterFactory(MoshiConverterFactory.create(moshi))
}
}

return generateRetrofitBuilder(retrofitBuilder)
.build().create(service)
Expand Down

0 comments on commit ff1b310

Please sign in to comment.