首页 > 解决方案 > 在 Android 中使用 Retrofit 获取请求的表单数据

问题描述

我从根本上误解了正在发生的事情。从我阅读和研究的内容来看,多部分和数据表单似乎用于发布请求,同时我需要在我的获取请求中添加一个令牌。

需要在 Android 中使用 Retrofit 和 Kotlin 重新创建此请求

curl --location --request GET 'localhost:5000/api/657' \
--form 'token="zjdW36WT0kkrlZHzjv3IpjysT9dA6QzK"

我的改装服务

@GET("api/{id}")
suspend fun request(@Path("id") id :  Long, @Part("token") token : RequestBody) : ServerResponse

我称之为有趣的地方,令牌是一个字符串

val tok: RequestBody = token.toRequestBody("text/plain".toMediaTypeOrNull())
val response: ServerResponse
try {
      response = retrofitService.request(id, tok)
}

标签: androidgetrequestretrofit2

解决方案


推荐阅读