Android Retrofit 2获得全身反应

时间:2015-10-21 05:46:29

标签: android retrofit okhttp

我目前遇到一个问题,我无法查看改装请求的完整响应,只看到了所有内容。

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

拨打api电话:

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

回调:

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

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

现在,当前通话仅输出:

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

这是因为我知道出了什么问题,但是无论出现什么问题都包含在响应中我只是无法弄清楚如何确切地看到内部服务器错误是什么。

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

@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();
        }
    }
}
傻了我

答案 1 :(得分:0)

我强烈建议您使用OkHttp HttpLoggingInterceptor

答案 2 :(得分:0)

这是我用来捕捉改造2错误的方法。

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

答案 3 :(得分:0)

改造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);

        }

目标代码的客户格式

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

}