使用 Retrofit 发送 FormUrlEncoded 时出现问题

Problems Sending FormUrlEncoded using Retrofit

我有这个请求,我需要使用 Retrofit 通过 FormUrlEncoded 发送它

{
   "clnt_id": "OQW",
   "clnt_res": "AA!@#$T",
   "type": "SCDS",
   "len": "ASD"
}

我使用了这个代码:

    @FormUrlEncoded
    @POST("Endpoint")
    @Headers("Accept: Application/JSON")
    fun connect(
        @Field("clnt_id") clnt_id: String,
        @Field(value = "clnt_res", encoded = false) clnt_res: String,
        @Field("type") type: String,
        @Field("len") len: String
    ): Observable<Token>

首先,请求没有发送为 JSON

二、"clnt_res"的值,通过retrofit编码

您有 2 个选项可以使用 Retrofit 从 android 发送 json 请求。

  1. 创建 json 请求的 Pojo 模型并通过设置它的值来传递它。
  2. 创建 HashMap 并将其传递给请求。

这是使用第二种方法的解决方案:

创建 hashmap 并放入键(参数)和值:

Map<String,String> requestMap = new HashMap<>();
        requestMap.put("clnt_id","your_value");
        requestMap.put("clnt_res","your_value");
        requestMap.put("type","your_value");
        requestMap.put("len","your_value");

然后使用 FieldMap 将其传递给您的改造请求:

@FormUrlEncoded
    @POST("Endpoint")
    @Headers("Accept: Application/JSON")
    fun connect(
        @FieldMap requestMap:Map<String,String>
    ): Observable<Token>

我终于得到了答案,这是 'clnt_res'"AA!@#$T" 中符号 '$' 的问题,问题出在 kotlin 中以转义特殊字符,您需要这样做"$",我做的IDE没告诉我错的是这个"$/".