时间:2021-03-27来源:www.pcxitongcheng.com作者:电脑系统城
基于 kotlin/coroutine/retrofit/jetpack 打造,100来行代码,用法超级简单舒适
设置默认Retrofit工厂和全局错误处理程序
| 1 2 3 4 5 |
HttpCall.init(retrofitFactory = { // ...}, errorHandler = { throwable -> // ...}) |
基本用法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 方法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
| 1 2 3 4 5 6 |
repositories { maven { url "https://gitee.com/ezy/repo/raw/android_public/"}} dependencies { implementation "me.reezy.jetpack:httpcall:0.4.0"} |
2026-06-09
win10系统以太网连接图标不见了怎么办 Win10 / Win11 找不到以太网通用解决方法2024-04-11
台式机电脑如何连接外接显示器2024-04-11
小新系列打印机手机配置网络的方法教程一、40系列之后机型 X(250、240s、240、230s) X1 carbon(20BT/20BS) YOGA 11e T(550、540p、450s、450、440s、440p、440、431s) W(550s、540、541) L(450、440、550) E(550、55...
2024-04-11
ThinkPad蓝牙鼠标如何配对解答步骤41U5008鼠标驱动官网地址: https://support.lenovo.com/en_US/downloads/detail.page?&LegacyDocID=MIGR-67201 第一种方式是比较传统的:使...
2024-04-11