仅具有表单数据的翻新2 [英] Retrofit 2 with only form-data

查看:80
本文介绍了仅具有表单数据的翻新2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用翻新2发出POST请求.请求类型为form-data application/x-www-form-urlencoded.

I am trying to make POST request using the Retrofit 2. The request type is form-data NOT application/x-www-form-urlencoded.

我只发布数据,而不是请求中的文件,并且响应采用JSON形式.

I am only posting data not the files in the request and the response is in the form of JSON.

我已经尝试过@FormUrlEncoded, @Multipart,但是它不起作用.

I have tried @FormUrlEncoded, @Multipart but it is not working.

我尝试了以下请求

1.第一次尝试

@FormUrlEncoded
@POST("XXXX")
Call<PlanResponse> getPlanName(@Field(Constants.ACTION_ID) String actionId, @Field(Constants.OFFER_CODE) String offerCode);

2.第二次尝试

@Headers({"Content-Type: multipart/form-data","Content-Type: text/plain"})
@FormUrlEncoded
@POST("XXXX")
Call<PlanResponse> getPlans(@Body @FieldMap(encoded = false) Map<String, String> data);

3.三次尝试

@Headers("Content-Type: multipart/form-data")
@Multipart
@POST("XXXX")
Call<PlanResponse> myPlans(@Part(Constants.ACTION_ID) String actionId, @Part(Constants.OFFER_CODE) String offerCode);

我只是将身体设为 null .它正在与POSTMAN一起使用.

I am only getting the body as null. It is working with the POSTMAN.

我还搜索了form-dataapplication/x-www-form-urlencoded,发现如果数据为二进制,则使用form-data,如果数据为ASCII,则使用application/x-www-form-urlencoded

I have also search about form-data and application/x-www-form-urlencoded and found that if the data is binary then use form-data and if data is ASCII then use application/x-www-form-urlencoded

我正在尝试找到改造不支持表单数据吗?

POSTMAN请求

Cache-Control: no-cache
Postman-Token: XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXX
Content-Type: multipart/form-data; boundary=----    WebKitFormBoundaryXXXXXXXXXXXX


 ----WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="actionId"

1000
 ----WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="offerCode"

MYCODE
----WebKitFormBoundaryXXXXXXXXXXXX

我只能添加从POSTMAN中截取的HTTP生成的代码

I can only add HTTP Generated code snipped from POSTMAN

推荐答案

在翻新版2.0中,如上执行POST请求,您应该像这样对参数使用RequestBody类型.

In retrofit 2.0 to perform POST request like above, you should use RequestBody type for your parameter like this.

@Multipart
@POST("XXXX")
Call<PlanResponse> myPlans(@Part(Constants.ACTION_ID) RequestBody actionId, @Part(Constants.OFFER_CODE) RequestBody offerCode);

这里是如何从String获取requestBody.

And here how to get requestBody from String.

String somevalue = "somevalue";
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), somevalue);

这篇关于仅具有表单数据的翻新2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆