Android使用 Coroutine + Retrofit打造簡單的HTTP請(qǐng)求庫
基于 kotlin/coroutine/retrofit/jetpack 打造,100來行代碼,用法超級(jí)簡單舒適
設(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. 用xslt+css讓RSS顯示的跟網(wǎng)頁一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)4. 移動(dòng)端HTML5實(shí)現(xiàn)拍照功能的兩種方法5. 測試模式 - XSL教程 - 56. ASP.NET Core自定義中間件的方式詳解7. html5手機(jī)觸屏touch事件介紹8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. 教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)
