Android使用 Coroutine + Retrofit打造簡單的HTTP請求庫
基于 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' }
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效5. 使用css實(shí)現(xiàn)全兼容tooltip提示框6. Python編寫nmap掃描工具7. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解8. .NET6打包部署到Windows Service的全過程9. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享10. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值
