Android使用 Coroutine + Retrofit打造簡單的HTTP請求庫
更新時間:2021年03月27日 10:17:37 作者:ezy
這篇文章主要介紹了Android使用 Coroutine + Retrofit打造簡單的HTTP請求庫,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
基于 kotlin/coroutine/retrofit/jetpack 打造,100來行代碼,用法超級簡單舒適
設(shè)置默認(rèn)Retrofit工廠和全局錯誤處理程序
HttpCall.init(retrofitFactory = {
// ...
}, errorHandler = { throwable ->
// ...
})
基本用法
data class Reault(val data:String)
interface TestService {
@GET("test")
fun test(): Call<Reault>
}
// 在 activity/fragment 中使用,獲取請求結(jié)果
http<TestService>().test().result(this) {
// it 是 Reault
}
// 在 activity/fragment 中使用,獲取請求響應(yīng)對象
http<TestService>().test().response(this) {
// it 是 Response<Result>
}
顯示請求狀態(tài),基于 HttpCall擴(kuò)展出 withSpinning 方法
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)
引入
https://github.com/czy1121/httpcall
repositories {
maven { url "https://gitee.com/ezy/repo/raw/android_public/"}
}
dependencies {
implementation "me.reezy.jetpack:httpcall:0.4.0"
}
您可能感興趣的文章:
- Android ViewModel的使用總結(jié)
- Android Jetpack架構(gòu)組件 ViewModel詳解
- 解決android viewmodel 數(shù)據(jù)刷新異常的問題
- Android-ViewModel和LiveData使用詳解
- Android通過ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁面的數(shù)據(jù)共享功能
- Android使用Retrofit上傳文件功能
- android Retrofit2網(wǎng)絡(luò)請求封裝介紹
- Android Retrofit框架的使用
- Github簡單易用的?Android?ViewModel?Retrofit框架
相關(guān)文章
Android學(xué)習(xí)教程之日歷控件使用(7)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之日歷控件操作代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android 7.0 運(yùn)行時權(quán)限彈窗問題的解決
這篇文章主要介紹了Android 7.0 運(yùn)行時權(quán)限彈窗問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對象的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對象的方法,結(jié)合實(shí)例形式分析了Android全局Context的獲取及Intent傳遞對象的具體操作方法,需要的朋友可以參考下2017-08-08
Android activity堆棧及管理實(shí)例詳解
這篇文章主要介紹了Android activity堆棧及管理實(shí)例詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,對android activity堆棧相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-09-09

