网络请求、JSON 文件读取解析
例子代码
data class LoginRequest(val username: String, val password: String)
data class LoginResponse(val token: String)
interface AuthService {
@POST("auth/login")
@Headers("Content-Type: application/json")
suspend fun login(@Body request: LoginRequest): LoginResponse
}
object RetrofitClient {
private lateinit var retrofit: Retrofit
fun initialize(baseUrl: String) {
retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val authService: AuthService by lazy {
retrofit.create(AuthService::class.java)
}
val userService: UserService by lazy {
retrofit.create(UserService::class.java)
}
}
interface UserService {
@GET("user/data")
suspend fun getUserData(@Header(