国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

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

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

基于 kotlin/coroutine/retrofit/jetpack 打造,100來行代碼,用法超級簡單舒適

設置默認Retrofit工廠和全局錯誤處理程序

HttpCall.init(retrofitFactory = { // ...}, errorHandler = { throwable -> // ...})

基本用法

data class Reault(val data:String)interface TestService { @GET('test') fun test(): Call<Reault> } // 在 activity/fragment 中使用,獲取請求結果http<TestService>().test().result(this) { // it 是 Reault}// 在 activity/fragment 中使用,獲取請求響應對象http<TestService>().test().response(this) { // it 是 Response<Result>}

顯示請求狀態,基于 HttpCall擴展出 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
相關文章:
主站蜘蛛池模板: 东乡族自治县| 齐齐哈尔市| 鄯善县| 巢湖市| 阳西县| 永修县| 四平市| 彭水| 临夏县| 乌拉特后旗| 甘谷县| 勐海县| 连云港市| 湄潭县| 本溪市| 修武县| 慈利县| 盘山县| 体育| 兴国县| 塔河县| 马山县| 大洼县| 济南市| 通化市| 靖宇县| 安泽县| 繁昌县| 汕头市| 英德市| 察隅县| 务川| 垫江县| 仪陇县| 彝良县| 天长市| 秦皇岛市| 句容市| 克拉玛依市| 渭源县| 武义县|