KHttp Kotlin DSL封装网络请求库

Kotlin 的网络求写起来总是啰里啰嗦?你可以试试KHttp。这是个用 Kotlin DSL 包装过的OkHttp3库,用起来挺舒服,写网络求跟写配置一样自然。

链式调用的风格,代码写起来清爽,KHttp.get(...).enqueue { ... }一看就知道是干嘛的,不用再绕一堆 builder。你要是对响应做,状态码、响应体、头信息都好拿,连Gson解析也都内建好了。

异步/同步都有,配合 Kotlin 协程用也顺。类型安全也不错,Kotlin 的静态类型让你少踩不少坑。你还能通过它原生支持的缓存机制提速,尤其适合做图片或分页列表加载。

写法也简单,像这样:

KHttp.get("https://api.example.com/data")
  .enqueue { response ->
    if (response.isSuccessful) {
      val data = response.bodyAsJson(MyData::class.java)
      // 数据
    } else {
      // 错误
    }
  }

如果你已经用惯了OkHttp3,那 KHttp 其实就是个更顺手的壳,能让你少写不少模板代码。如果你对 Kotlin DSL 感兴趣,也可以看看像Retrofit 的 Kotlin DSL 定制Recyclical这样的库,都是同一个思路。

,如果你在 Kotlin 项目里做网络求还在堆 builder、封装 CallBack,那KHttp值得你试一试。用起来顺手,改起来轻松。

zip
KHttp.zip 预估大小:53个文件
folder
KHttp-master 文件夹
file
.gitignore 86B
folder
gradle 文件夹
folder
wrapper 文件夹
file
gradle-wrapper.jar 52KB
file
gradle-wrapper.properties 230B
file
README.md 3KB
file
build.gradle 1KB
file
gradlew.bat 2KB
file
gradlew 5KB
folder
app 文件夹
file
.gitignore 7B
file
build.gradle 1KB
file
proguard-rules.pro 944B
folder
src 文件夹
folder
test 文件夹
folder
java 文件夹
folder
vip 文件夹
folder
frendy 文件夹
folder
khttpdemo 文件夹
file
ExampleUnitTest.java 398B
folder
main 文件夹
file
AndroidManifest.xml 993B
folder
java 文件夹
folder
vip 文件夹
folder
frendy 文件夹
folder
khttpdemo 文件夹
folder
net 文件夹
file
Constants.kt 372B
file
Request.kt 3KB
file
RequestCommon.kt 662B
folder
mock 文件夹
file
MockServer.kt 2KB
file
MainActivity.kt 5KB
file
JavaActivity.java 1KB
folder
entity 文件夹
file
Resp.kt 532B
file
News.kt 803B
folder
util 文件夹
file
Handler.kt 418B
file
DeviceInfo.kt 315B
folder
res 文件夹
folder
values 文件夹
file
styles.xml 383B
file
colors.xml 208B
file
strings.xml 72B
folder
mipmap-xxhdpi 文件夹
file
ic_launcher_round.png 10KB
file
ic_launcher.png 8KB
folder
layout 文件夹
file
activity_main.xml 2KB
file
activity_java.xml 241B
folder
mipmap-xhdpi 文件夹
file
ic_launcher_round.png 6KB
file
ic_launcher.png 5KB
folder
mipmap-hdpi 文件夹
file
ic_launcher_round.png 4KB
file
ic_launcher.png 3KB
folder
mipmap-mdpi 文件夹
file
ic_launcher_round.png 2KB
file
ic_launcher.png 2KB
folder
mipmap-xxxhdpi 文件夹
file
ic_launcher_round.png 14KB
file
ic_launcher.png 10KB
folder
androidTest 文件夹
folder
java 文件夹
folder
vip 文件夹
folder
frendy 文件夹
folder
khttpdemo 文件夹
file
ExampleInstrumentedTest.java 744B
file
gradle.properties 730B
folder
khttp 文件夹
file
.gitignore 7B
file
build.gradle 1KB
file
proguard-rules.pro 944B
folder
src 文件夹
folder
main 文件夹
file
AndroidManifest.xml 114B
folder
java 文件夹
folder
vip 文件夹
folder
frendy 文件夹
folder
khttp 文件夹
file
KHttpClient.kt 775B
file
KUpload.kt 3KB
folder
model 文件夹
file
UploadProgressInterceptor.java 975B
file
CountingRequestBody.java 2KB
file
KRequest.kt 3KB
file
KSocket.kt 3KB
file
KDownload.kt 3KB
folder
res 文件夹
folder
values 文件夹
file
strings.xml 68B
file
LICENSE.txt 1KB
file
settings.gradle 25B
...
zip 文件大小:148.16KB