Android使用 Coroutine + Retrofit打造簡(jiǎn)單的HTTP請(qǐng)求庫(kù)
基于 kotlin/coroutine/retrofit/jetpack 打造,100來(lái)行代碼,用法超級(jí)簡(jiǎn)單舒適
設(shè)置默認(rèn)Retrofit工廠和全局錯(cuò)誤處理程序
HttpCall.init(retrofitFactory = { // ...}, errorHandler = { throwable -> // ...})
基本用法
data class Reault(val data:String)interface TestService { @GET('test') fun test(): Call<Reault> } // 在 activity/fragment 中使用,獲取請(qǐng)求結(jié)果http<TestService>().test().result(this) { // it 是 Reault}// 在 activity/fragment 中使用,獲取請(qǐng)求響應(yīng)對(duì)象http<TestService>().test().response(this) { // it 是 Response<Result>}
顯示請(qǐng)求狀態(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' }
相關(guān)文章:
1. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式2. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法3. html中的form不提交(排除)某些input 原創(chuàng)4. 使用css實(shí)現(xiàn)全兼容tooltip提示框5. 利用CSS制作3D動(dòng)畫6. XML解析錯(cuò)誤:未組織好 的解決辦法7. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單8. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器9. jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法10. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))
