前言

一般安卓开发中网络请求框架都是使用OkHttp+Retrofit,但其只能应用于jvm平台上

而Kotlin可用于jvm,js,native

因此一个Kotlin跨平台的网络请求框架必不可少

而ktor-client是Kotlin官方提供的网络请求框架,可以跨平台的进行网络请求(官网) 与之对应的还有ktor-server可以用来写服务器端(但这不是这篇文章的重点)

但ktor-client使用起来还是稍微有些麻烦,所以我们可以模仿Retrofit开发一个更方便使用的跨平台网络请求框架(单平台也可以使用)

也就是接下来的主角:懒人Http框架,地址: LazyPeopleHttp · GitHub

其不仅比Retrofit使用更方便,而且性能更高(因为用编译时替代了运行时反射)

正文

//下面代码相当于: https://xxx/getUser?userId=$userId

suspend fun getUser(userId: Int): User

fun getUser(userId: Int): Call

//如果使用Compose可以这样用:

val user by remember { hf.getUser(0).toState() }

Text("UserName=${user?.name}")

使用方式

Step 1.添加依赖:

如果是单平台,在app模块目录内的build.gradle.kts内添加

plugins {

...

id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.8.20对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases

}

dependencies {

...

implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:1.0.9")//最新版本可以看github地址

ksp("io.github.ltttttttttttt:LazyPeopleHttp:1.0.9")

}

如果是多平台,在common模块目录内的build.gradle.kts内添加

plugins {

...

id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.8.20对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases

}

...

val commonMain by getting {

dependencies {

...

api("io.github.ltttttttttttt:LazyPeopleHttp-lib:1.0.9")

}

}

...

dependencies {

add("kspCommonMainMetadata", "io.github.ltttttttttttt:LazyPeopleHttp:1.0.9")

}

如果你使用的ksp版本小于1.0.9则需要以下配置:

ksp配置

Step 2.接口声明:

@LazyPeopleHttpService

interface HttpFunctions {

//标准post请求声明

@POST("post/postB")

fun postB(@Field("name") t: String): Call

//懒人post请求声明,会把方法名当做url,其下划线会转换为斜杠

fun post_postC(name: String): Call

//suspend post请求声明

suspend fun post_postA(t: String): String

//标准get请求声明

@GET("get/getA")

fun getA(@Query("t") t2: String): Call

//懒人get请求声明

fun get_getB(name: String): Call

//suspend get请求声明

suspend fun suspendGetB(name: String): UserBean

//添加静态的请求头

@Header("aaa", "bbb")

fun post_checkHeader(): Call

//配置动态的url

@GET("get/getD/{type}")

fun getD(@Url("type") url: String): Call

//可以声明具体函数,此时不会生成额外的方法

fun ccc(): Int = 0

}

Step 3.接口使用:

//配置ktor的client

private val client = HttpClient {

defaultRequest {

//配置baseUrl

url("http://127.0.0.1:666/")

}

}

private val config = LazyPeopleHttpConfig(client)

//创建请求接口的实现类

private val hf = HttpFunctions::class.createService(config)

//使用接口的实现类

hf.postB("123").enqueue()//回调异步请求

hf.suspendGetB("111")//协程异步请求

val data by remember { hf.get().toState() }//返回响应式的State,适用于Compose

Step 4.自定义配置:

/*

* 当前LazyPeopleHttpService类全局配置

* [client]ktor请求客户端

* [serializer]序列化器

* [encryptor]加解密器

* [defaultRequestMethod]默认请求方式(不使用注解的方法)

* [onSuspendError]suspend函数抛出异常时调用

* [onRequest]成功构造了请求,但发送请求之前调用

* [onResponse]请求之后调用

*/

class LazyPeopleHttpConfig(...)

//单独修改一个接口的配置

hf.postB("123").config {

//this is HttpRequestBuilder

}.enqueue()

ksp {

//开启运行时配置获取所有注解的功能,不开启时调用[RequestInfo#functionAnnotations]始终返回null

//arg("getFunAnnotationsWithLazyPeopleHttp", "true")

//你甚至可以修改创建Call的方法,来返回自定义的Call

//arg("createCallFunNameWithLazyPeopleHttp", "CallAdapter.createCall2")

}

参考阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。