改造 POST 请求 response.isSuccessful() 返回 false [英] Retrofit POST request response.isSuccessful() returning false
问题描述
我正在尝试使用 Retrofit2 发出 POST
请求.但我处于 response.isSuccessful()
返回 false 的情况,我不知道如何调试它.我检查了后端的日志,没有错误,什么都没有.
I am trying to make POST
request using Retrofit2. But I am in a situation where the response.isSuccessful()
is returning false and I do not know how to debug it. I checked my backend's logs and there are no errors, nothing.
我确保请求的 URL 正确,并且所有参数都正确.我做错了什么?对我来说,该请求似乎甚至没有发送到我的服务器.
I made sure that the request's URL is correct, also that all the parameters are right. What I am doing wrong? For me it seems that the request even doesn't make it to my server.
这就是我所做的:
我有带有 getter 和 setter 的 User 类:
I have class User with getters and setters:
public class User {
@SerializedName("user_id")
@Expose
private int id;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
我有我的服务:
public interface APIService {
@POST("auth/register")
@FormUrlEncoded
Call<User> register(@Field("email") String email, @Field("password") String password);
}
现在我尝试执行它,它在响应块中返回 false:
Now I try to execute it, which returns false in response block:
private void registerUser(){
Call<User> registerCall = apiService.register(email, password);
registerCall.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccessful()){
int id = response.body().getId();
String firstName = response.body().getFirstName();
String lastName = response.body().getLastName();
}else{
System.out.println("ERROR "+response.raw().body());
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
}
这是 ApiUtils:
This is the ApiUtils:
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "https://api.mydomain.com/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
我做错了什么?我怎样才能做更多的调试?任何帮助表示赞赏.
What I am doing wrong? How I can do more debugging? Any help is appreciated.
推荐答案
构建改造实例(您的 getApiService 方法)似乎有问题,要查看 api 调用的日志和状态,您可以使用 http 日志记录拦截器作为:
It seems to be problem with building retrofit instance(your getApiService method), and to see the log and status of api calls you can use http logging interceptor as:
private static Retrofit retrofit = null;
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new LogJsonInterceptor());
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
您可以使用拦截器查看 API 的原始 JSON 响应.希望这可以解决您的问题.
you can see the raw JSON response of API using interceptor. Hope this may solve your problem.
这篇关于改造 POST 请求 response.isSuccessful() 返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!