Gson无法解析json中的字节数组

qcbq4gxm  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(181)

使用一个旧的http库,不能按照说明使用一个新的。为什么当pojo定义了字节数组时,gson需要一个对象。
我的代码:

private EternalAsyncHttpClient client = new EternalAsyncHttpClient();
  RequestParams params = new RequestParams();
                params.put("LocationName","Document Returned"); //?LocationName=Document Returned
                client.get(GET_PDF_DETAILS_API,params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        PDFResponse pdfResponse = gson.fromJson(new String(responseBody), PDFResponse.class);
                        Debug.i("pdf: success ");
    }on Failure...

Pojo等级:

public class PDFResponse {

    private Integer pdfconfigid;
    private String locationName;
    private String imgPath;
    private byte [] imgBinaryData;
    private String locationCode;
    private String activeStatus;
    private String address;
    public Integer getPdfconfigid() {
        return pdfconfigid;
    }
    public void setPdfconfigid(Integer pdfconfigid) {
        this.pdfconfigid = pdfconfigid;
    }
    public String getLocationName() {
        return locationName;
    }
    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }
    public String getImgPath() {
        return imgPath;
    }
    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
    public String getLocationCode() {
        return locationCode;
    }
    public void setLocationCode(String locationCode) {
        this.locationCode = locationCode;
    }
    public String getActiveStatus() {
        return activeStatus;
    }
    public void setActiveStatus(String activeStatus) {
        this.activeStatus = activeStatus;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public byte[] getImgBinaryData() {
        return imgBinaryData;
    }

    public void setImgBinaryData(byte[] imgBinaryData) {
        this.imgBinaryData = imgBinaryData;
    }
}

错误:

E/AsyncHttpRH: User-space exception detected!
    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:221)
        at com.google.gson.Gson.fromJson(Gson.java:861)

User-space exception detected!错误只发生在我尝试使用gson将json转换为Object时,所以它很可能是一个错误,没有转换代码的gson没有异常。
世珍:

[
    {
        "pdfconfigid": 6,
        "LocationName": "Document Returned",
        "imgPath": "~/PdfReportCompanyLogo/6/places-to-visit-in-west-india.jpg",
        "imgBinaryData": [-1,40,-1],
        "LocationCode": "LOC0008",
        "ActiveStatus": "Active",
        "address": "address"
    }
]

更新:使用

@SerializedName("imgBinaryData")
public List<Integer> imgBinaryData = null;

也不起作用。

lrl1mhuk

lrl1mhuk1#

使用以下类,因为您POJO与JSON响应不匹配,例如在JSON响应中您具有ActiveStatus,但在POJO中它是activeStatus

public class Example {

@SerializedName("pdfconfigid")

private int pdfconfigid;
@SerializedName("LocationName")

private String locationName;
@SerializedName("imgPath")

private String imgPath;
@SerializedName("imgBinaryData")

private List<Integer> imgBinaryData = null;
@SerializedName("LocationCode")

private String locationCode;
@SerializedName("ActiveStatus")

private String activeStatus;
@SerializedName("address")

private String address;

public int getPdfconfigid() {
return pdfconfigid;
}

public void setPdfconfigid(int pdfconfigid) {
this.pdfconfigid = pdfconfigid;
}

public String getLocationName() {
return locationName;
}

public void setLocationName(String locationName) {
this.locationName = locationName;
}

public String getImgPath() {
return imgPath;
}

public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}

public List<Integer> getImgBinaryData() {
return imgBinaryData;
}

public void setImgBinaryData(List<Integer> imgBinaryData) {
this.imgBinaryData = imgBinaryData;
}

public String getLocationCode() {
return locationCode;
}

public void setLocationCode(String locationCode) {
this.locationCode = locationCode;
}

public String getActiveStatus() {
return activeStatus;
}

public void setActiveStatus(String activeStatus) {
this.activeStatus = activeStatus;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}

相关问题