这是一个利用Retrofit+协程+ViewModel的便捷、精简发起网络请求的工具
implementation 'io.github.conerjoy:Speedy:1.0.5'
Speedy.instance.init(this) {
baseUrl("http://v.juhe.cn/")
addConverterFactory(GsonConverterFactory.create())
}
// Speedy.instance.init(this, mRetrofit)
Speedy.instance.showToast = {
ToastUtils.showShort(it)
}
Speedy.NETWORK_ERROR_TOAST = true // 全局生效
open class ResponseResult<T> : SpeedyBean<T> {
var reason: String = ""
var result: T? = null
var error_code: Int = -1
override fun isSuccess() : Boolean = error_code == 0
override fun message(): String = reason
}
class MainViewModel : BaseVM() {
private val _result = MutableLiveData<String>()
val result: LiveData<String> = _result
fun getResult() {
launch<ResponseResult<Any>> {
invoke {
buildService<Test>().getSomething(mapOf("key" to "xxxx"))
}.onSuccess {
_result use result.toString()
}.onApiError {
LogUtils.e("业务错误")
}.onError {
LogUtils.e("请求错误")
LogUtils.e(message)
}.onComplete {
LogUtils.e("请求结束")
}
}
}
@JvmSuppressWildcards
interface Test {
@FormUrlEncoded
@POST("toutiao/index")
suspend fun getSomething(@FieldMap map: Map<String, Any>): ResponseResult<Any>
}
}
如果对您有价值、有帮助,请怒点star