How do I get a response from a post request if the response is different from the post class

Onatemowo Damilare :

I'm using retrofit to pass login details to my server through an api. The post request for the api takes in just email and password but the response returns a different Json format from what the POJO class contains. How do I handle the api response?

I've tried returning the response as a JSONObject to help get the Json from the api but it doesn't work. The API returns a success json containing the user name and a login token.

    Call<LoginPost> call = apiLink.loginUser(useremail, userpassword);

    call.enqueue(new Callback<LoginPost>() {
        @Override
        public void onResponse(Call<LoginPost> call, Response<LoginPost> response) {
            if(!response.isSuccessful()){
                String code = Integer.toString(response.code());
                Toast.makeText(LoginPage.this, code, Toast.LENGTH_LONG).show();
            }
            else {
             LoginPost postResponse = response.body();

             Log.e("viewResponse", 
                   postResponse.getSuccessResponse().toString());

               return;
            }
        }

        @Override
        public void onFailure(Call<LoginPost> call, Throwable t) {
            Log.e("error in createNewUser",  t.getMessage());
        }
    });

the post class:

@SerializedName("email")
String userEmail;


@SerializedName("password")
String userPassword;

public JSONObject getSuccessResponse() {
    return successResponse;
}

@SerializedName("success")
JSONObject successResponse;


public String getUserEmail() {
    return userEmail;
}


public String getUserPassword() {
    return userPassword;
}
Jlange :

Rather than using the POJO class for the Request when making the Retrofit call, you should use a POJO class matching the Response. Because this is just using parameters to make the call, you probably don't even need a Request object, but there is no harm in having one.

Your code would look something like this:

Call<LoginResponse> call = apiLink.loginUser(useremail, userpassword);

call.enqueue(new Callback<LoginResponse>() {
    @Override
    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
        if(!response.isSuccessful()){
            String code = Integer.toString(response.code());
            Toast.makeText(LoginPage.this, code, Toast.LENGTH_LONG).show();
        }
        else {
         LoginResponse postResponse = response.body();

         Log.e("viewResponse", 
               postResponse.getSuccessResponse().toString());

           return;
        }
    }

    @Override
    public void onFailure(Call<LoginResponse> call, Throwable t) {
        Log.e("error in createNewUser",  t.getMessage());
    }
});

To further explain what is happening, when you create your parameterized call you are telling Retrofit which Object to use to parse the Response, (if you want to use an object as post body data, you need to declare your API differently):

 @POST("auth/login")
 Call<LoginResponse> loginUser(@Body LoginPost body);

 Call<LoginResponse> call = apiLink.loginUser(LoginPost body);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156557&siteId=1