无法解析完整响应的Gson转换器改造

时间:2019-10-03 06:24:49

标签: android gson retrofit2

我的服务器响应:

{
  "serverResponseData": {
    "Status": "Success",
    "Message": ""
  },
  "tripHistoryData": [
    {
      "intRstId": "-2147400808",
      "intRstVehId": "-2146930960",
      "RosterDate": "30-Sep-2019",
      "ShiftDate": "30-Sep-2019",
      "ShiftTime": "1:00PM",
      "Verified": "No",
      "PickDrop": "Pick",
      "FirstPickLastDropAddress": "Borivali Rly Stn",
      "RouteName": "SHU PICK 00:02",
      "ExpectedStartTime": "2019-09-30T10:00:21",
      "ExpectedEndTime": "2019-09-30T12:30:00",
      "DriverAppUsed": "Yes",
      "TripEndedInApp": "No",
      "ActualStartTime": "30-Sep-2019 11:34AM",
      "ActualEndTime": "",
      "PlannedKM": "68.915 km",
      "ApprovedKM": "Pending for Approval",
      "OverSpeedingPenalty": "No",
      "OtherPenalties": "",
      "PenaltyAmount": "0.00"
    }
  ]
}

我的模型班:

public class TripHistoryResponse{
  @SerializedName("serverResponseData")
  @Expose
  private ServerResponseData serverResponseData;
  @SerializedName("tripHistoryData")
  @Expose
  private ArrayList<TripHistoryData> tripHistoryData;
  public void setServerResponse(ServerResponseData serverResponseData){
   this.serverResponseData = serverResponseData;
  }
  public ServerResponseData getServerResponse(){
   return serverResponseData;
  }
  public void setTripHistoryData(ArrayList<TripHistoryData> tripHistoryData){
   this.tripHistoryData=tripHistoryData;
  }
  public ArrayList<TripHistoryData> getTripHistoryData(){
   return tripHistoryData;
  }
}

public class ServerResponseData {
  @SerializedName("Status")
  @Expose
  private String Status;
  @SerializedName("Message")
  @Expose
  private String Message;
  public void setStatus(String Status){
   this.Status=Status;
  }
  public String getStatus(){
   return Status;
  }
  public void setMessage(String Message){
   this.Message=Message;
  }
  public String getMessage(){
   return Message;
  }
}

public class TripHistoryData{
  @SerializedName("ApprovedKM")
  @Expose
  private String ApprovedKM;
  @SerializedName("TripEndedInApp")
  @Expose
  private String TripEndedInApp;
  @SerializedName("OverSpeedingPenalty")
  @Expose
  private String OverSpeedingPenalty;
  @SerializedName("OtherPenalties")
  @Expose
  private String OtherPenalties;
  @SerializedName("intRstId")
  @Expose
  private String intRstId;
  @SerializedName("FirstPickLastDropAddress")
  @Expose
  private String FirstPickLastDropAddress;
  @SerializedName("DriverAppUsed")
  @Expose
  private String DriverAppUsed;
  @SerializedName("ExpectedEndTime")
  @Expose
  private String ExpectedEndTime;
  @SerializedName("PickDrop")
  @Expose
  private String PickDrop;
  @SerializedName("Verified")
  @Expose
  private String Verified;
  @SerializedName("ExpectedStartTime")
  @Expose
  private String ExpectedStartTime;
  @SerializedName("ActualStartTime")
  @Expose
  private String ActualStartTime;
  @SerializedName("RosterDate")
  @Expose
  private String RosterDate;
  @SerializedName("PlannedKM")
  @Expose
  private String PlannedKM;
  @SerializedName("intRstVehId")
  @Expose
  private String intRstVehId;
  @SerializedName("ActualEndTime")
  @Expose
  private String ActualEndTime;
  @SerializedName("PenaltyAmount")
  @Expose
  private String PenaltyAmount;
  @SerializedName("ShiftDate")
  @Expose
  private String ShiftDate;
  @SerializedName("RouteName")
  @Expose
  private String RouteName;
  @SerializedName("ShiftTime")
  @Expose
  private String ShiftTime;
  public void setApprovedKM(String ApprovedKM){
   this.ApprovedKM=ApprovedKM;
  }
  public String getApprovedKM(){
   return ApprovedKM;
  }
  public void setTripEndedInApp(String TripEndedInApp){
   this.TripEndedInApp=TripEndedInApp;
  }
  public String getTripEndedInApp(){
   return TripEndedInApp;
  }
  public void setOverSpeedingPenalty(String OverSpeedingPenalty){
   this.OverSpeedingPenalty=OverSpeedingPenalty;
  }
  public String getOverSpeedingPenalty(){
   return OverSpeedingPenalty;
  }
  public void setOtherPenalties(String OtherPenalties){
   this.OtherPenalties=OtherPenalties;
  }
  public String getOtherPenalties(){
   return OtherPenalties;
  }
  public void setIntRstId(String intRstId){
   this.intRstId=intRstId;
  }
  public String getIntRstId(){
   return intRstId;
  }
  public void setFirstPickLastDropAddress(String FirstPickLastDropAddress){
   this.FirstPickLastDropAddress=FirstPickLastDropAddress;
  }
  public String getFirstPickLastDropAddress(){
   return FirstPickLastDropAddress;
  }
  public void setDriverAppUsed(String DriverAppUsed){
   this.DriverAppUsed=DriverAppUsed;
  }
  public String getDriverAppUsed(){
   return DriverAppUsed;
  }
  public void setExpectedEndTime(String ExpectedEndTime){
   this.ExpectedEndTime=ExpectedEndTime;
  }
  public String getExpectedEndTime(){
   return ExpectedEndTime;
  }
  public void setPickDrop(String PickDrop){
   this.PickDrop=PickDrop;
  }
  public String getPickDrop(){
   return PickDrop;
  }
  public void setVerified(String Verified){
   this.Verified=Verified;
  }
  public String getVerified(){
   return Verified;
  }
  public void setExpectedStartTime(String ExpectedStartTime){
   this.ExpectedStartTime=ExpectedStartTime;
  }
  public String getExpectedStartTime(){
   return ExpectedStartTime;
  }
  public void setActualStartTime(String ActualStartTime){
   this.ActualStartTime=ActualStartTime;
  }
  public String getActualStartTime(){
   return ActualStartTime;
  }
  public void setRosterDate(String RosterDate){
   this.RosterDate=RosterDate;
  }
  public String getRosterDate(){
   return RosterDate;
  }
  public void setPlannedKM(String PlannedKM){
   this.PlannedKM=PlannedKM;
  }
  public String getPlannedKM(){
   return PlannedKM;
  }
  public void setIntRstVehId(String intRstVehId){
   this.intRstVehId=intRstVehId;
  }
  public String getIntRstVehId(){
   return intRstVehId;
  }
  public void setActualEndTime(String ActualEndTime){
   this.ActualEndTime=ActualEndTime;
  }
  public String getActualEndTime(){
   return ActualEndTime;
  }
  public void setPenaltyAmount(String PenaltyAmount){
   this.PenaltyAmount=PenaltyAmount;
  }
  public String getPenaltyAmount(){
   return PenaltyAmount;
  }
  public void setShiftDate(String ShiftDate){
   this.ShiftDate=ShiftDate;
  }
  public String getShiftDate(){
   return ShiftDate;
  }
  public void setRouteName(String RouteName){
   this.RouteName=RouteName;
  }
  public String getRouteName(){
   return RouteName;
  }
  public void setShiftTime(String ShiftTime){
   this.ShiftTime=ShiftTime;
  }
  public String getShiftTime(){
   return ShiftTime;
  }
}

改造网络通话:

public void getPastHistory(String vehicleNum, final PastTripsCallback callback) {

        Call<TripHistoryResponse> call = networkService.getTripHistory(vehicleNum);

        call.enqueue(new Callback<TripHistoryResponse>() {
            @Override
            public void onResponse(Call<TripHistoryResponse> call, Response<TripHistoryResponse> response) {
                Log.d(TAG, "getPastHistory() onResponse : " + response.code());

                try {
                    if (response != null && response.isSuccessful() && response.body() != null && response.body().getServerResponse() != null &&
                            response.body().getServerResponse().getStatus() != null &&
                            response.body().getServerResponse().getStatus().equalsIgnoreCase(Constant.SUCCESS)) {
                        callback.onSuccess(response.body());
                        return;
                    } else if (response != null && response.isSuccessful() && response.body() != null && response.body().getServerResponse() != null &&
                            response.body().getServerResponse().getStatus() != null &&
                            response.body().getServerResponse().getStatus().equalsIgnoreCase(Constant.FAIL)) {
                        callback.onError(response.body().getServerResponse().getMessage(), Constant.SHOW_SERVER_FAIL_TO_CONTACT_MESSAGE_ON_UI);
                        return;
                    }
                } catch (Exception e) {
                    callback.onError(new NetworkError(e), Constant.SHOW_SERVER_FAIL_TO_CONTACT_MESSAGE_ON_UI);
                    Log.e(TAG, "onResponse: exception :: " + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<TripHistoryResponse> call, Throwable t) {
                Log.e(TAG, "onFailure: Exception bla bla ......: " + t.getMessage());
                callback.onError(new NetworkError(t), Constant.SHOW_SERVER_FAIL_TO_CONTACT_MESSAGE_ON_UI);
            }
        });
    }

Exception that i am getting : java.lang.NullPointerException: throw with null exception
        at b.b.a.a.e.h.a(Unknown Source:3)
        at b.b.a.a.g.f.a(Unknown Source:68)
        at h.l.run(Unknown Source:37)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

注意:我仅在发布模式下遇到异常,在调试模式下出现崩溃或异常。 我能够访问tripHistoryData(ArrayList),但是当我从ServerResponseData类访问Status时,我得到了NullPointerException。我将整个TripHistoryResponse类记录到json中,并发现serverResponseData类为空json。

出了什么问题无法理解。

2 个答案:

答案 0 :(得分:1)

例如,您的JSON响应有错误:

这个

{
  serverResponseData: {
    Status: "Success",
    Message: "",

  },

应如下所示:

{
  "serverResponseData": {
    "Status": "Success",
    "Message": ""

  },

尝试阅读此JSON format

我建议始终期望响应变量为@Nullable,并始终在使用它们之前进行空检查。

以下是Response模型在Java中的外观:

public class Response {
    @SerializedName("serverResponseData")
    @Nullable ServerResponseData serverResponseData;

    @SerializedName("tripHistoryData")
    @Nullable List<TripHistoryData> tripHistoryData;
}

class ServerResponseData {
    @SerializedName("Status")
    @Nullable String status = "";
    @SerializedName("Message")
    @Nullable String message = "";
}

class TripHistoryData {
.......
}

答案 1 :(得分:0)

在模型类中添加了@Keep注释对我有用。