【发布时间】:2016-01-05 03:21:06
【问题描述】:
我是 Retrofit 的新手。我尝试在我的项目中实施 Retrofit 2.0.0 测试版。为了注册用户,我尝试了 Retrofit。方法 POST 是,内容类型是 application/x-www-form-urlencoded。我收到 400 的响应,但响应正文是 null。我在这里附上我的代码。
public interface MyApiInterface {
@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("/users/register")
Call<LoginResponse> doRegister(@Field("key") String value, @Field("key")
String value;
}
public class RestClient {
private static Retrofit REST_CLIENT;
private static MyApiInterface apiService;
public static String BASE_URL = "http://xx.xxx.xxx.xx:";
static {
setupRestClient();
}
public static MyApiInterface get() {
return apiService;
}
private static void setupRestClient() {
REST_CLIENT = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService =
REST_CLIENT.create(MyApiInterface.class);
}
}
Call<Register> call = RestClient.get().doRegister("xxx",""xxxx);
call.enqueue(new Callback<Register>() {
@Override
public void onResponse(Response<Register> response, Retrofit retrofit) {
int statusCode = response.code();
LoginResponse user = response.body();
Log.d("String",statusCode+""+response.body() );
}
@Override
public void onFailure(Throwable t) {
}
});
我得到了回应
{
status: "failed"
error_code: "invalid_client"
}
我的注册课程是
public class Register {
public String status;
public String error_code;
}
【问题讨论】:
标签: android web-services gson response retrofit