久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Android使用 Coroutine + Retrofit打造簡單的HTTP請求庫

瀏覽:6日期:2022-09-20 10:12:05

基于 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' }

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 中牟县| 科尔| 新河县| 张掖市| 嵩明县| 斗六市| 安丘市| 新宁县| 万载县| 龙南县| 大港区| 县级市| 揭西县| 绍兴市| 中阳县| 瑞丽市| 乐清市| 高邑县| 德阳市| 和顺县| 澄江县| 汾阳市| 福清市| 罗平县| 玉田县| 长春市| 兖州市| 定安县| 康定县| 巴楚县| 科尔| 葵青区| 洮南市| 靖州| 安平县| 奎屯市| 南川市| 安远县| 萝北县| 高雄县| 建始县|