Skip to content

Instantly share code, notes, and snippets.

@CreateChance
Last active December 20, 2023 03:46
Show Gist options
  • Save CreateChance/09d03374f7bbe19bf316fcfc7ce377e0 to your computer and use it in GitHub Desktop.
Save CreateChance/09d03374f7bbe19bf316fcfc7ce377e0 to your computer and use it in GitHub Desktop.
The most elegant practice found so far is to use retrofit, gson converter, and kudos to ensure the empty security of kotlin data.
plugins {
kotlin("jvm")
id("com.kanyun.kudos") version "1.9.20-1.1.1"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kudos {
gson = true
}
dependencies {
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:3.14.9")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(11)
}
package org.example
import com.google.gson.annotations.SerializedName
import com.kanyun.kudos.annotations.Kudos
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
/**
* retrofit + gson converter + kudos 来做 kotlin 数据的空安全保证
* 目前找到的最优雅的实践
*/
fun main() {
val response = serverApi.getData().execute()
if (response.isSuccessful) {
println("请求成功,http code: ${response.code()}, message: ${response.message()}")
val serverResponse = response.body()
println("请求到的数据: $serverResponse")
} else {
println("失败了!http code: ${response.code()}, message: ${response.message()}")
}
}
@Kudos
data class Data(
@SerializedName("id")
val id: Int,
@SerializedName("imagePath")
val imagePath: String,
@SerializedName("url")
val url: String,
@SerializedName("not_existed_field")
val noExistedField: String = "default value" // 服务器端不会返回这个数据,kudos 会使用这个默认值,而不是 null,确保空安全
)
data class ServerResponse(
@SerializedName("errorCode")
val code: Int,
@SerializedName("errorMsg")
val message: String,
@SerializedName("data")
val dataList: List<Data>
)
interface IServerApi {
@GET("banner/json")
fun getData(): Call<ServerResponse>
}
val serverApi: IServerApi by lazy {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
val retrofit = Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://www.wanandroid.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofit.create(IServerApi::class.java)
}
12月 20, 2023 11:42:36 上午 okhttp3.internal.platform.Platform log
信息: --> GET https://www.wanandroid.com/banner/json
12月 20, 2023 11:42:36 上午 okhttp3.internal.platform.Platform log
信息: <-- 200 OK https://www.wanandroid.com/banner/json (235ms, unknown-length body)
请求成功,http code: 200, message: OK
请求到的数据: ServerResponse(code=0, message=, dataList=[Data(id=30, imagePath=https://www.wanandroid.com/blogimgs/42da12d8-de56-4439-b40c-eab66c227a4b.png, url=https://www.wanandroid.com/blog/show/3352, noExistedField=default value), Data(id=6, imagePath=https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png, url=https://www.wanandroid.com/navi, noExistedField=default value), Data(id=10, imagePath=https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png, url=https://www.wanandroid.com/blog/show/2, noExistedField=default value)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment