使用retrofit2

时间:2017-06-02 07:41:18

标签: android retrofit2

我有一个端点,我在其中输入一些数据,其中Content-Type应该是form-data。

我正在使用

 @FormUrlEncoded
 @PUT("/api/v1/clients/profile/all_details")
 Call<ResponseBody> postUserProfile(@FieldMap Map<String, String> userParams);

但在发送请求时,表单数据的编码类似于

food_allergy=%5Beggs%2C%20milk%2C%20nuts%2C%20none%5D&diet_prefer=Non%20Vegetarian&age=25&exercise_level=Moderate&email=Email&name=Veeresh&height=175&prompt-qgreet3=I%27m%20ready%21&gender=Female&health_condition=%5Bdiabetes%2C%20PCOD%5D&weight=69

如何删除编码?

我试过这个博客

https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded-using-fieldmap

3 个答案:

答案 0 :(得分:0)

如果您不想对表单数据进行编码,那么您可以尝试这样

@PUT("/api/v1/clients/profile/all_details") Call<ResponseBody> postUserProfile(@QueryMap Map<String, String> userParams);

编辑1

@Multipart
        @PUT("/api/v1/clients/profile/all_details")
        Call<ResponseBody> postUserProfile(@Part(username) RequestBody username, @Part(password) RequestBody password);

以下是如何从String获取requestBody。

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

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

答案 1 :(得分:0)

您首先可以使用

替换所有%
string.replace("%","");

以同样的方式完成其余的工作。

答案 2 :(得分:0)

@FieldMap有一个名为encoded的元素,默认为false

如果您将其设置为true,则说明我的数据已经过编码,我不希望Retrofit处理编码。

@FieldMap(encoded = true) Map<String, String> userParams