下面给出 **Fuel 2.x** 的 **“开箱即用”** 封装类,**同时支持**:

下面给出 Fuel 2.x"开箱即用" 封装类,同时支持

  • POST JSONapplication/json
  • POST 表单application/x-www-form-urlencoded
  • 文件上传multipart/form-data

复制即用零配置自动切回主线程回调


✅ 1. 添加依赖(build.gradle)

kotlin 复制代码
implementation 'com.github.kittinunf.fuel:fuel:2.3.1'
implementation 'com.github.kittinunf.fuel:fuel-android:2.3.1'   // 自动切主线程
implementation 'com.github.kittinunf.fuel:fuel-gson:2.3.1'      // JSON 自动序列化

✅ 2. 封装类(copy 即用)

kotlin 复制代码
object HttpFuel {

    /* ① POST JSON ---------------------------------------------------------- */
    fun postJson(
        url: String,
        body: Any,                      // 任意对象 / Map / String
        onSuccess: (String) -> Unit,
        onError: (String) -> Unit
    ) {
        Fuel.post(url)
            .jsonBody(Gson().toJson(body))        // 自动转 JSON
            .responseString { _, _, result ->
                val (data, error) = result
                if (error == null) onSuccess(data!!)
                else onError(error.message)
            }
    }

    /* ② POST 表单 ---------------------------------------------------------- */
    fun postForm(
        url: String,
        params: List<Pair<String, Any?>>,
        onSuccess: (String) -> Unit,
        onError: (String) -> Unit
    ) {
        Fuel.post(url, params.filterNotNull())
            .responseString { _, _, result ->
                val (data, error) = result
                if (error == null) onSuccess(data!!)
                else onError(error.message)
            }
    }

    /* ③ 上传文件(可带表单字段) ------------------------------------------- */
    fun upload(
        url: String,
        params: List<Pair<String, Any?>> = emptyList(),
        file: File,
        fileKey: String = "file",
        onProgress: (Long, Long) -> Unit = { _, _ -> },
        onSuccess: (String) -> Unit,
        onError: (String) -> Unit
    ) {
        Fuel.upload(url, Method.POST, params)
            .source { _, _ -> file }
            .name { fileKey }
            .progress { read, total -> onProgress(read, total) }
            .responseString { _, _, result ->
                val (data, error) = result
                if (error == null) onSuccess(data!!)
                else onError(error.message)
            }
    }
}

✅ 3. 使用示例(Activity / ViewModel)

kotlin 复制代码
/* 3.1 发 JSON */
HttpFuel.postJson(
    url = "http://192.168.1.100:8809/sync/one688",
    body = mapOf("ben" to ben)        // 任意对象/Map
) { resp -> Log.d("JSON", resp) }

/* 3.2 发表单 */
HttpFuel.postForm(
    url = "http://192.168.1.100:8809/sync/one688",
    params = listOf("id" to 0, "tlite" to "text")
) { resp -> Log.d("FORM", resp) }

/* 3.3 上传文件 + 进度 */
val file = File(cacheDir, "a.jpg")
HttpFuel.upload(
    url = "http://192.168.1.100:8809/upload",
    params = listOf("user" to "tom"),
    file = file,
    onProgress = { read, total ->
        val pct = (read * 100 / total).toInt()
        Log.d("UP", "$pct%")
    }
) { resp -> Log.d("UP", "完成:$resp") }

✅ 4. 自动切回主线程

fuel-android 模块已保证 回调在主线程可直接更新 UI


✅ 5. 自定义全局配置(可选)

kotlin 复制代码
FuelManager.instance.basePath = "http://192.168.1.100:8809/"
FuelManager.instance.baseHeaders = mapOf("token" to "abc123")

🔚 一句话总结

引入 fuel + fuel-android + fuel-gson复制 HttpFuel 单例
postJson / postForm / upload 三种调用 开箱即用主线程回调零配置发请求

相关推荐
小孔龙3 小时前
GraphicBuffer 跨进程共享
android
行业研究员4 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter5 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者85 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者87 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin7 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数
wxson728221 小时前
【Android视频监控系统技术总结】
android·音视频
hunterandroid1 天前
[Android 从零到一] LiveData 粘性事件与单次消费:从重复触发到可靠事件总线
android
hunterandroid1 天前
[Android 从零到一] Android 事件分发机制:从 ACTION_DOWN 到 View 事件消费链路
android