Android volley无法将api响应转换为嵌套实体

时间:2016-06-02 09:50:25

标签: android android-volley gson

我面临将api响应转换为实体对象的问题。

protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        return Response.success(gson.fromJson(jsonString, clazz), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    }
}

响应进入jsonString但return Response.success(gson.fromJson(jsonString, clazz), HttpHeaderParser.parseCacheHeaders(response));

以上行无法将空洞响应转换为嵌套实体类。 如果需要更多信息,请告诉我。 JSON响应

{
"tripId": 81204,
"tripMapDetails": {
    "category": "Successful Trips",
    "categoryImage": "/Icons/HDPIURL/places.png",
    "sourceLattitude": -25.969816207885742,
    "sourceLongitude": 28.129779815673828,
    "destLattitude": -26.1075663,
    "destLongitude": 28.0567007
},
"tripDriverDetails": {
    "category": "Driver summary",
    "categoryImage": "/Icons/HDPIURL/ic_car_tab.png",
    "driverName": "Winjit-Test-Driver1",
    "carName": "Corolla Toyota",
    "rating": 0,
    "driverPhoto": "~/Images/Drivers/7_Zebra-no-fin.jpg",
    "avgRating": 4.6,
    "carRegistrationnumber": "FB86HDGP",
    "driverId": 289
},
"tripRideDetails": {
    "distance": "0 km",
    "distanceCharge": "0",
    "durationCharge": "50",
    "category": "Ride summary",
    "categoryImage": "/Icons/HDPIURL/ride_summary.png",
    "tripDate": "27 May 2016",
    "tripStartTime": "10:17",
    "tripEndTime": "10:17",
    "duration": "0.13 min",
    "fare": "50",
    "isCancelled": false,
    "sourceAddress": "Pretoria Main Rd,  Midrand,  South Africa",
    "destinationAddress": "Sandton, Gauteng, South Africa",
    "promoCode": "",
    "discountAmt": "0",
    "isPaywithCash": true,
    "finalAmount": "50",
    "currency": "R",
    "jobId": 81204,
    "paymentMethod": "Cash",
    "carType": ""
}
}

类,

public class GetTripDetails {

@SerializedName("tripId")
@Expose
private Integer tripId;
@SerializedName("tripMapDetails")
@Expose
private TripMapDetails tripMapDetails;
@SerializedName("tripDriverDetails")
@Expose
private TripDriverDetails tripDriverDetails;
@SerializedName("tripRideDetails")
@Expose
private TripRideDetails tripRideDetails;

/**
 * 
 * @return
 *     The tripId
 */
public Integer getTripId() {
    return tripId;
}

/**
 * 
 * @param tripId
 *     The tripId
 */
public void setTripId(Integer tripId) {
    this.tripId = tripId;
}

/**
 * 
 * @return
 *     The tripMapDetails
 */
public TripMapDetails getTripMapDetails() {
    return tripMapDetails;
}

/**
 * 
 * @param tripMapDetails
 *     The tripMapDetails
 */
public void setTripMapDetails(TripMapDetails tripMapDetails) {
    this.tripMapDetails = tripMapDetails;
}

/**
 * 
 * @return
 *     The tripDriverDetails
 */
public TripDriverDetails getTripDriverDetails() {
    return tripDriverDetails;
}

/**
 * 
 * @param tripDriverDetails
 *     The tripDriverDetails
 */
public void setTripDriverDetails(TripDriverDetails tripDriverDetails) {
    this.tripDriverDetails = tripDriverDetails;
}

/**
 * 
 * @return
 *     The tripRideDetails
 */
public TripRideDetails getTripRideDetails() {
    return tripRideDetails;
}

/**
 * 
 * @param tripRideDetails
 *     The tripRideDetails
 */
public void setTripRideDetails(TripRideDetails tripRideDetails) {
    this.tripRideDetails = tripRideDetails;
}

}

内部课程,

public class TripMapDetails {

@SerializedName("category")
@Expose
private String category;
@SerializedName("categoryImage")
@Expose
private String categoryImage;
@SerializedName("sourceLattitude")
@Expose
private float sourceLattitude;
@SerializedName("sourceLongitude")
@Expose
private float sourceLongitude;
@SerializedName("destLattitude")
@Expose
private float destLattitude;
@SerializedName("destLongitude")
@Expose
private float destLongitude;

/**
 * 
 * @return
 *     The category
 */
public String getCategory() {
    return category;
}

/**
 * 
 * @param category
 *     The category
 */
public void setCategory(String category) {
    this.category = category;
}

/**
 * 
 * @return
 *     The categoryImage
 */
public String getCategoryImage() {
    return categoryImage;
}

/**
 * 
 * @param categoryImage
 *     The categoryImage
 */
public void setCategoryImage(String categoryImage) {
    this.categoryImage = categoryImage;
}

/**
 * 
 * @return
 *     The sourceLattitude
 */
public float getSourceLattitude() {
    return sourceLattitude;
}

/**
 * 
 * @param sourceLattitude
 *     The sourceLattitude
 */
public void setSourceLattitude(float sourceLattitude) {
    this.sourceLattitude = sourceLattitude;
}

/**
 * 
 * @return
 *     The sourceLongitude
 */
public float getSourceLongitude() {
    return sourceLongitude;
}

/**
 * 
 * @param sourceLongitude
 *     The sourceLongitude
 */
public void setSourceLongitude(float sourceLongitude) {
    this.sourceLongitude = sourceLongitude;
}

/**
 * 
 * @return
 *     The destLattitude
 */
public float getDestLattitude() {
    return destLattitude;
}

/**
 * 
 * @param destLattitude
 *     The destLattitude
 */
public void setDestLattitude(float destLattitude) {
    this.destLattitude = destLattitude;
}

/**
 * 
 * @return
 *     The destLongitude
 */
public float getDestLongitude() {
    return destLongitude;
}

/**
 * 
 * @param destLongitude
 *     The destLongitude
 */
public void setDestLongitude(float destLongitude) {
    this.destLongitude = destLongitude;
}

}

1 个答案:

答案 0 :(得分:1)

分享你的json和对象类。您的json和类中可能存在数据类型不匹配。在json到gson转换周围放置一个try catch块,看看在解析时是否有任何异常,如果你得到任何异常则发布堆栈跟踪。

修改 - 1

尝试放置此块

try{ gson.fromJson(jsonString, clazz) }catch(Exception e){ e.printStacktrace}

之前

return Response.success(gson.fromJson(jsonString, clazz), HttpHeaderParser.parseCacheHeaders(response));

检查您的日志,看看您是否收到任何例外情况。您的任何类中的数据类型可能不匹配。如果你得到任何记录,请在此发布,以便我可以帮助你。

编辑2 它为我工作。我已经写了下面的课程。

public class MyClass {
    private int tripId;
    private TripMapDetails tripMapDetails;
    private TripDriverDetails tripDriverDetails;
    private TripRideDetails tripRideDetails;`
}

public class TripMapDetails {
    private String category;
    private String categoryImage;
    private float sourceLattitude;
    private float sourceLongitude;
    private float destLattitude;
    private float destLongitude;
}

public class TripDriverDetails {
    private String category;
    private String categoryImage;
    private String driverName;
    private String carName;
    private int rating;
    private String driverPhoto;
    private float avgRating;
    private String carRegistrationnumber;
    private int driverId;
}

public class TripRideDetails {
    private String distanceCharge;
    private String durationCharge;
    private String category;
    private String categoryImage;
    private String tripDate;
    private String tripStartTime;
    private String tripEndTime;
    private String duration;
    private String fare;
    private boolean isCancelled;
    private String sourceAddress;
    private String destinationAddress;
    private String promoCode;
    private String discountAmt;
    private boolean isPayWithCash;
    private String finalAmount;
    private String currency;
    private int jobId;
    private String paymentMethod;
    private String carType;
}

执行以下代码并能够获取所有数据。

try{
        MyClass myclass = gson.fromJson(json,MyClass.class);
        Log.e("-trip id-",""+myclass.getTripId());
        Log.e("-car name-",""+myclass.getTripDriverDetails().getCarName());
        Log.e("-reg name-",""+myclass.getTripDriverDetails().getCarRegistrationnumber());
        Log.e("-category-",""+myclass.getTripDriverDetails().getCategory());
        Log.e("-cat img-",""+myclass.getTripDriverDetails().getCategoryImage());
        Log.e("-driver name-",""+myclass.getTripDriverDetails().getDriverName());
        Log.e("-dr photo-",""+myclass.getTripDriverDetails().getDriverPhoto());
        Log.e("-avg rating-",""+myclass.getTripDriverDetails().getAvgRating());
        Log.e("-dri id-",""+myclass.getTripDriverDetails().getDriverId());
        Log.e("-rating-",""+myclass.getTripDriverDetails().getRating());
        Log.e("------------","------------------------------------------------------------");
        Log.e("-map det cat img-",""+myclass.getTripMapDetails().getCategoryImage());
        Log.e("- map det category-",""+myclass.getTripMapDetails().getCategory());
        Log.e("-dest lat-",""+myclass.getTripMapDetails().getDestLattitude());
        Log.e("-dest long-",""+myclass.getTripMapDetails().getDestLongitude());
        Log.e("-src lat-",""+myclass.getTripMapDetails().getSourceLattitude());
        Log.e("-src long-",""+myclass.getTripMapDetails().getSourceLongitude());
        Log.e("------------","------------------------------------------------------------");
        Log.e("-ride category-",""+myclass.getTripRideDetails().getCategory());
        Log.e("-ride cat img-",""+myclass.getTripRideDetails().getCategoryImage());
        Log.e("-car type-",""+myclass.getTripRideDetails().getCarType());
        Log.e("-currency-",""+myclass.getTripRideDetails().getCurrency());
        Log.e("-dest addr-",""+myclass.getTripRideDetails().getDestinationAddress());
        Log.e("-disc amt-",""+myclass.getTripRideDetails().getDiscountAmt());
        Log.e("-dist charge-",""+myclass.getTripRideDetails().getDistanceCharge());
        Log.e("-duration-",""+myclass.getTripRideDetails().getDuration());
        Log.e("-duration charge-",""+myclass.getTripRideDetails().getDurationCharge());
        Log.e("-fare-",""+myclass.getTripRideDetails().getFare());
        Log.e("-final amt-",""+myclass.getTripRideDetails().getFinalAmount());
        Log.e("-payment method-",""+myclass.getTripRideDetails().getPaymentMethod());
        Log.e("-promo code-",""+myclass.getTripRideDetails().getPromoCode());
        Log.e("-src addr-",""+myclass.getTripRideDetails().getSourceAddress());
        Log.e("-trip date-",""+myclass.getTripRideDetails().getTripDate());
        Log.e("-trip end time-",""+myclass.getTripRideDetails().getTripEndTime());
        Log.e("-trip start time-",""+myclass.getTripRideDetails().getTripStartTime());
        Log.e("-trip details job id-",""+myclass.getTripRideDetails().getJobId());
        Log.e("-trip details cartype-",""+myclass.getTripRideDetails().getCarType());
        Log.e("- cancelled-",""+myclass.getTripRideDetails().isCancelled());
        Log.e("- pay with cash-",""+myclass.getTripRideDetails().isPayWithCash());

    }catch (Exception e){
        e.printStackTrace();
    }