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. Intellij IDEA連接Navicat數(shù)據(jù)庫(kù)的方法2. 關(guān)于Mysql-connector-java驅(qū)動(dòng)版本問(wèn)題總結(jié)3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. CSS自定義滾動(dòng)條樣式案例詳解5. 詳解JavaScript作用域、作用域鏈和閉包的用法6. python 批量下載bilibili視頻的gui程序7. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)8. 通過(guò)工廠模式返回Spring Bean方法解析9. Ajax提交post請(qǐng)求案例分析10. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
