Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

Retrofit POST request response.isSuccessful() returning 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.

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.

This is what I do:

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

And I have my service:

public interface APIService {

    @POST("auth/register")
    @FormUrlEncoded
    Call<User> register(@Field("email") String email, @Field("password") String password);
}

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) {

            }
        });
    }

Edit: 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.

Upvotes: 2

Views: 6791

Answers (1)

TankRaj
TankRaj

Reputation: 89

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

you can see the raw JSON response of API using interceptor. Hope this may solve your problem.

Upvotes: 1

Related Questions