【发布时间】:2018-12-17 05:53:34
【问题描述】:
当我过去使用改造通过 Api 发布数据时,当我在界面中使用 @FormUrlEncoded 但在邮递员中我使用表单数据发布时,我收到响应 400,你能帮帮我吗
这是我的界面
public interface SignupApiService {
@POST("users")
@FormUrlEncoded
Call<ResponseBody> savePost(@Field("email") String email,
@Field("password") String password,
@Field("role") int role);
}
这是我的注册活动
Call<ResponseBody> call = SignupClient
.getInstance().getApi()
.savePost(email, password, roleValue);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int statusCode = response.code();
progressBar.setVisibility(View.GONE);
Log.d("SignupActivity", "onResponse" +statusCode);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这是我的 Java 类
public class SignupClient {
private static SignupClient mInstance;
private static Retrofit retrofit;
private static final String BASE_URL = "http://74.207.233.160/api/v1/";
private SignupClient(){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized SignupClient getInstance() {
if (mInstance == null){
mInstance = new SignupClient();
}
return mInstance;
}
public SignupApiService getApi() {
return retrofit.create(SignupApiService.class);
}
}
在这里,当我使用 formurlencoded 时,我得到响应 400。 如何在此处使用formurlencoded 的表单数据。 或者我的邮递员需要进行任何更改
【问题讨论】:
-
这和 JavaScript 有什么关系??
-
你的反手可能有错误?
-
只要去掉 FormDataUrl 注释,它仍然可以工作。 Postman 中的 form-data 用于将数据作为 Multipart 表单数据发送,我认为它不是 @FormUrlEncoded。你能发布你的后端 API 代码吗?
-
你试过在邮递员中设置 content-type
application/x-www-form-urlencoded吗?
标签: java android retrofit retrofit2