Based on kotlin/coroutine/retrofit/jetpack, 100 lines of code, use super easy and comfortable
Set up the default Retrofit factory and global error handler
HttpCall.init(retrofitFactory = {
// ...
}, errorHandler = { throwable ->
// ...
})
Copy the code
Basic usage
data class Reault(val data:String)
interface TestService {
@GET("test")
fun test(a): Call<Reault>
}
// Use it in an activity/fragment to get the result of the request
http<TestService>().test().result(this) {
/ / it is Reault
}
// Used in activity/fragment to get the request response object
http<TestService>().test().response(this) {
/ / it is the Response < Result >
}
Copy the code
Displays the status of the request, extending the withSpinning method based on HttpCall
fun <T : Any> HttpCall<T>.withSpinning(activity: FragmentActivity, spinning: Boolean = false, text: String = ""): HttpCall<T> {
activity.apply {
if (isFinishing || isDestroyed) return@apply
val dialog = showLoading(spinning, text)
finally { dialog.dismiss() }
}
return this
}
http<TestService>().test().result(this) {
Log.e("api", it.data)
}.withSpinning(this)
Copy the code
The introduction of
Github.com/czy1121/htt…
repositories { maven { url "https://gitee.com/ezy/repo/raw/android_public/"} } dependencies { implementation "Me. Reezy. Jetpack: httpcall: 0.4.0"}Copy the code