【发布时间】:2019-10-26 15:25:01
【问题描述】:
我是Kotlin 和Retrofit 的新手。我想通过Retrofit 调用基础URL 并打印原始JSON 响应。什么是最简单的最小配置?
比方说,
base url = "https://devapis.gov/services/argonaut/v0/"
method = "GET"
resource = "Patient"
param = "id"
我试过了,
val patientInfoUrl = "https://devapis.gov/services/argonaut/v0/"
val infoInterceptor = Interceptor { chain ->
val newUrl = chain.request().url()
.newBuilder()
.query(accountId)
.build()
val newRequest = chain.request()
.newBuilder()
.url(newUrl)
.header("Authorization",accountInfo.tokenType + " " + accountInfo.accessToken)
.header("Accept", "application/json")
.build()
chain.proceed(newRequest)
}
val infoClient = OkHttpClient().newBuilder()
.addInterceptor(infoInterceptor)
.build()
val retrofit = Retrofit.Builder()
.baseUrl(patientInfoUrl)
.client(infoClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
Logger.i(TAG, "Calling retrofit.create")
try {
// How to get json data here
}catch (e: Exception){
Logger.e(TAG, "Error", e);
}
Logger.i(TAG, "Finished retrofit.create")
}
我怎样才能得到原始的 json 输出。如果可能的话,我不想实现用户数据类和解析东西。有什么办法吗?
更新 1
标记为重复的帖子 (Get raw HTTP response with Retrofit) 不适用于 Kotlin,我需要 Kotlin 版本。
【问题讨论】:
-
如果您只想打印响应,那么为什么不使用
HttpLogIntercepter?获取原始回复Follow this .. -
@ADM 我不知道
HttpLogIntercepter。 -
看看this
-
@HemantParmar 这是在 java 中,我正在使用 Kotlin。
-
Marked duplicated post (Get raw HTTP response with Retrofit) is not for Kotlin and I need Kotlin version.然后转换它。我喜欢你的简历中所说的“问题解决者”,而你却依赖其他人为你将 Java 代码转换为 Kotlin
标签: android rest kotlin retrofit http-get