【问题标题】:Retrofit POST request response.isSuccessful() returning false改造 POST 请求 response.isSuccessful() 返回 false
【发布时间】:2017-09-02 10:49:37
【问题描述】:

我正在尝试使用 Retrofit2 发出 POST 请求。但我处于response.isSuccessful() 返回错误的情况,我不知道如何调试它。我检查了后端的日志,没有错误,什么都没有。

我确保请求的 URL 正确,并且所有参数都正确。我做错了什么?对我来说,这个请求似乎连我的服务器都没有。

这就是我的工作:

我有带有 getter 和 setter 的 User 类:

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:

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:

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);
    }
}

我做错了什么?我怎样才能做更多的调试?任何帮助表示赞赏。

【问题讨论】:

  • apiService的值是多少请贴出完整代码
  • @ArpitPrajapati apiService的值为ApiUtils.getAPIService();
  • APIService 接口中添加 ResObject 尝试将其替换为 User
  • @ArpitPrajapati 哎呀,这是一个错字,它实际上是我的代码中的用户。
  • 您的代码看起来不错,请使用您从 api 获取的 json 数据检查您的 @SerializedName

标签: java android retrofit2


【解决方案1】:

构建改造实例(您的 getApiService 方法)似乎有问题,要查看 api 调用的日志和状态,您可以使用 http 日志拦截器:

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 响应。希望这可以解决您的问题。

【讨论】:

  • interceptors 不公开,因此您无法在包外访问它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多