我有这个请求,我需要使用 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 请求。
- 创建json请求的Pojo模型并通过设置它的值来传递它。
- 创建 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>
关于java - 使用 Retrofit 发送 FormUrlEncoded 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815528/