2015-10-21 106 views
0

我目前有一个问题,我无法查看改造请求的完整响应,只有一切似乎。Android Retrofit 2得到全身响应

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

这是因为我知道什么是错误的,但无论错误是否包含在响应中,我只是无法确定如何看到内部服务器错误是什么。

任何帮助,将不胜感激

回答

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

我傻

0

我强烈建议你使用OkHttpHttpLoggingInterceptor

+0

我不能包括在我的项目?当试图向我的依赖 –

+0

添加'compile'c​​om.squareup.okhttp:logging-interceptor:2.5.0''时,只是在解决错误时才解决错误,它仅在2.6.0-SNAPSHOT版本中可用 –

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

}