Joe Maher
Joe Maher

Reputation: 5460

Android Retrofit 2 get full body response

Im currently having an issue where i cant view the full response of a retrofit request, only everything but it seems.

My ApiManager:

public class ApiManager {

public interface AmbiApi {

    @POST("users/register")
    Call<User> registerUser(@Body User user);
}

private static final String API_URL = "http://api.staging.ambiapp.com/v1/";

private static final Retrofit RETROFIT = new Retrofit.Builder()
        .baseUrl(API_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

private static final AmbiApi AMBI_API = RETROFIT.create(AmbiApi.class);

public static AmbiApi getApi() {
    return AMBI_API;
}

Making the api call:

Call<User> registerUserCall = ApiManager.getApi().registerUser(user);
registerUserCall.enqueue(registerCallback);

Callbacks:

@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
    Log.e("LOG", "Retrofit Response: " + response.raw().toString());
}

@Override
public void onFailure(Throwable t) {
    //
}

Now that current call is only outputting:

Retrofit Response: Response{protocol=http/1.1, code=500, message=Internal Server Error, url=http://api.staging.ambiapp.com/v1/users/register}

and that is because i know something is wrong, but whatever is going wrong is included in the response i just cant work out how to see exactly what the internal server error is.

Any help would be appreciated

Upvotes: 0

Views: 8578

Answers (4)

Vinod Joshi
Vinod Joshi

Reputation: 7862

retrofit 2

 // create upload service client
    APIInterface service =
            ServiceGenerator.createService(APIInterface.class);

Call<SubmitFormModel> call = service.upload(description, body, STATE_CD);

call.enqueue(new Callback<SubmitFormModel>() {
        @Override
        public void onResponse(Call<SubmitFormModel> call,
                               Response<SubmitFormModel> response) {
            Log.v("Upload", "success");

            if (mProgressDialog != null && mProgressDialog.isShowing())
                mProgressDialog.dismiss();

            System.out.println("response call " + call);
            System.out.println("response Response " + response.body().toString());

            SubmitFormModel result  =  response.body();
            System.out.println("response message " + result.getResponseData().getMessage());

              /* Create an Intent that will start the next Activity. */
            Intent mainIntent = new Intent(CaptureImgUpload.this
                    , SuccessScreenActivity.class);
            CaptureImgUpload.this.startActivity(mainIntent);

        }

Customer format for object code

 public class SubmitFormModel {

private int statusCode;
private String status;
private ResponseData responseData;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public int getStatusCode() {
    return statusCode;
}

public void setStatusCode(int statusCode) {
    this.statusCode = statusCode;
}

public ResponseData getResponseData() {
    return responseData;
}

public void setResponseData(ResponseData responseData) {
    this.responseData = responseData;
}

public class ResponseData {

    private String message;
    public Data data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

public class Data {

    private int application_id;

    public int getApplication_id() {
        return application_id;
    }
    public void setApplication_id(int application_id) {
        this.application_id = application_id;
    }
}

}

Upvotes: 0

Rohit P Soman
Rohit P Soman

Reputation: 1161

This is the method I am using for catching retrofit 2 errors.

Call List<data> call=MyService.loadData();
    call.enqueue(new Callback<List<data>>() {
            @Override
            public void onResponse(Response<List<data>> response, Retrofit retrofit) {
                 if (response.isSuccess()) {
                    //Response success. Handle data here
                                }
                                else{
                             //For getting error message
                   Log.d("Error message",response.response.raw().message());  
                            //For getting error code. Code is integer value like 200,404 etc
                   Log.d("Error code",String.valueOf(response.response.raw().code()));        
                                }
           } 


            @Override
            public void onFailure(Throwable t) {
               if (t instanceof IOException){
                        //Add your code for displaying no network connection error   
                            } 
            }
        });

Upvotes: 0

guillaume_fr
guillaume_fr

Reputation: 501

I highly recommend you to use OkHttp HttpLoggingInterceptor.

Upvotes: 0

Joe Maher
Joe Maher

Reputation: 5460

@Override
public void onResponse(Response<User> response, Retrofit retrofit) {

    if (!response.isSuccess()) {
        try {
            Log.e("LOG", "Retrofit Response: " + response.errorBody().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

silly me

Upvotes: 1

Related Questions