-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add retrofit adapter:String and LiveData
- Loading branch information
xiaofu
committed
Dec 9, 2018
1 parent
b725143
commit fdbcc5b
Showing
10 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
lib_base/src/main/java/com/xiaofu/lib/base/http/HttpManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.xiaofu.lib.base.http | ||
|
||
import android.content.Context | ||
import com.readystatesoftware.chuck.ChuckInterceptor | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* 网络客户端 | ||
* Created by @author xiaofu on 2018/12/8. | ||
*/ | ||
class HttpManager private constructor() { | ||
|
||
private object Instance { | ||
val instance = HttpManager() | ||
} | ||
|
||
companion object { | ||
val INSTANCE = Instance.instance | ||
} | ||
|
||
/** | ||
* 方便开发者调试接口,使用前先 | ||
*/ | ||
private var chuckInterceptor: ChuckInterceptor? = null | ||
|
||
fun initChuck(context: Context) { | ||
chuckInterceptor = ChuckInterceptor(context) | ||
} | ||
|
||
/** | ||
* 配置OKHttp | ||
*/ | ||
private fun createOkHttpClient(): OkHttpClient { | ||
val builder = OkHttpClient.Builder() | ||
.connectTimeout(10, TimeUnit.SECONDS) | ||
.writeTimeout(10, TimeUnit.SECONDS) | ||
.readTimeout(30, TimeUnit.SECONDS) | ||
chuckInterceptor?.let { chuck -> | ||
builder.addInterceptor(chuck) | ||
} | ||
return builder.build() | ||
} | ||
|
||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.client(createOkHttpClient()) | ||
.baseUrl("http://apicloud.mob.com/") | ||
.addConverterFactory(StringConverterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
.addCallAdapterFactory(LiveDataCallAdapterFactory.create()) | ||
.build() | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
lib_base/src/main/java/com/xiaofu/lib/base/http/LiveDataCallAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* 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.xiaofu.lib.base.http | ||
|
||
|
||
import androidx.lifecycle.LiveData | ||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import java.lang.reflect.Type | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
/** | ||
* A Retrofit adapter that converts the Call into a LiveData of ApiResponse. | ||
* @param <R> | ||
</R> */ | ||
class LiveDataCallAdapter<R>(private val responseType: Type) : CallAdapter<R, LiveData<R>> { | ||
|
||
override fun responseType() = responseType | ||
|
||
override fun adapt(call: Call<R>): LiveData<R> { | ||
return object : LiveData<R>() { | ||
private var started = AtomicBoolean(false) | ||
override fun onActive() { | ||
super.onActive() | ||
if (started.compareAndSet(false, true)) { | ||
call.enqueue(object : Callback<R> { | ||
override fun onResponse(call: Call<R>, response: Response<R>) { | ||
postValue(response.body()) | ||
} | ||
|
||
override fun onFailure(call: Call<R>, throwable: Throwable) { | ||
postValue(null) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
lib_base/src/main/java/com/xiaofu/lib/base/http/LiveDataCallAdapterFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* 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.xiaofu.lib.base.http | ||
|
||
import androidx.lifecycle.LiveData | ||
import retrofit2.CallAdapter | ||
import retrofit2.CallAdapter.Factory | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
class LiveDataCallAdapterFactory : Factory() { | ||
override fun get( | ||
returnType: Type, | ||
annotations: Array<Annotation>, | ||
retrofit: Retrofit | ||
): CallAdapter<*, *>? { | ||
if (Factory.getRawType(returnType) != LiveData::class.java) { | ||
return null | ||
} | ||
|
||
if (returnType !is ParameterizedType) { | ||
throw IllegalArgumentException("resource must be parameterized") | ||
} | ||
|
||
val type = Factory.getParameterUpperBound(0, returnType) | ||
return LiveDataCallAdapter<Any>(type) | ||
} | ||
|
||
companion object { | ||
fun create(): LiveDataCallAdapterFactory { | ||
return LiveDataCallAdapterFactory() | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
lib_base/src/main/java/com/xiaofu/lib/base/http/StringConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.xiaofu.lib.base.http | ||
|
||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import java.io.IOException | ||
|
||
|
||
/** | ||
* | ||
* Created by @author xiaofu on 2018/12/8. | ||
*/ | ||
class StringConverter : Converter<ResponseBody, String> { | ||
|
||
@Throws(IOException::class) | ||
override fun convert(value: ResponseBody): String? { | ||
return value.string() | ||
} | ||
|
||
companion object { | ||
val INSTANCE = StringConverter() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
lib_base/src/main/java/com/xiaofu/lib/base/http/StringConverterFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.xiaofu.lib.base.http | ||
|
||
import retrofit2.Retrofit | ||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import java.lang.reflect.Type | ||
|
||
|
||
/** | ||
* | ||
* Created by @author xiaofu on 2018/12/8. | ||
*/ | ||
class StringConverterFactory : Converter.Factory() { | ||
|
||
override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *>? { | ||
return if (type == String::class.java) StringConverter.INSTANCE else null | ||
} | ||
|
||
companion object { | ||
fun create(): StringConverterFactory { | ||
return StringConverterFactory() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters