com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 3 列路径 $[0] Gson 错误中为 STRING

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $[0] Gson Error

每当我请求@Post 方法时就会发生错误。 @Get 方法可以正常工作。 PostMan 中的响应也很好。有人可以帮助我为什么会这样。 Headers 使用的是 header("Content-Type", "application/json") header("Accept", "application/json")

我的post请求方式-

@POST("api/product/getProductbyCategory")
    Observable<List<CategoryProductResponse>> getProductListOnCategory(@Query("category_id") String categoryId);

我收到回复 PostMan-

 [{"product_id":"50","name":"Lovinn Milk pouch","description":"Milk for daily use. Milk will be delivered next day of the order, Orders placed after 10:00 PM will ..","meta_title":"Milk pouch","meta_description":"Milk pouch","meta_keyword":"Milk pouch","tag":"Milk pouch","model":"Lovinn Milk Pouch","sku":"","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"BANGALORE","quantity":"10","stock_status":"Pre-Order","image":"catalog\/Milk_Pouch.png","manufacturer_id":"11","manufacturer":"Lovinn","price":"20.0000","special":null,"reward":null,"points":"0","tax_class_id":"9","date_available":"2019-06-22","weight":"0.00000000","weight_class_id":"1","length":"10.00000000","width":"0.00000000","length_class_id":"3","subtract":"1","rating":0,"reviews ":0,"sort_order":"1","viewed":"25"},{"product_id":"60","name":"Buffalo Milk 1 ltr","description":"Farm Fresh Pure Buffalo Raw Milk..","meta_title":"Farm Fresh Pure Buffalo Raw Milk","meta_description":"Farm Fresh Pure Buffalo Raw Milk","meta_keyword":"Farm Fresh Pure Buffalo Raw Milk","tag":"Farm Fresh Pure Buffalo Raw Milk","model":"Buffalo Milk One ltr","sku":"","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"5","stock_status":"2-3 Days","image":"catalog\/Buffalo-Milk.png","manufacturer_id":"11","manufacturer":"Lovinn","price":"40.0000","special":null,"reward":null,"points":"0","tax_class_id":"9","date_available":"2019-06-22","weight":"0.00000000","weight_class_id":"1","length":"0.00000000","width":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews ":0,"sort_order":"1","viewed":"7"},{"product_id":"57","name":"Lovinn 0.25 liter","description":"Milk for daily use. Milk will be delivered next day of the order, Orders placed after 10:00 PM will ..","meta_title":"Milk","meta_description":"Lovinn milk","meta_keyword":"cow milk","tag":"milk,cow milk, lovinn,","model":"Lovinn Milk Pouch1","sku":"","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"10","stock_status":"2-3 Days","image":"catalog\/Milk_Pouch.png","manufacturer_id":"11","manufacturer":"Lovinn","price":"10.0000","special":null,"reward":null,"points":"0","tax_class_id":"9","date_available":"2019-06-22","weight":"200.00000000","weight_class_id":"1","length":"0.00000000","width":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews ":0,"sort_order":"1","viewed":"7"},{"product_id":"59","name":"Buffalo Milk","description":"Farm Fresh Pure Buffalo Raw Milk..","meta_title":"Farm Fresh Pure Buffalo Raw Milk","meta_description":"Farm Fresh Pure Buffalo Raw Milk","meta_keyword":"Farm Fresh Pure Buffalo Raw Milk","tag":"Buffalo Raw Milk","model":"Buffalo Milk half ltr","sku":"","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"4","stock_status":"2-3 Days","image":"catalog\/Buffalo-Milk.png","manufacturer_id":"11","manufacturer":"Lovinn","price":"25.0000","special":null,"reward":null,"points":"0","tax_class_id":"9","date_available":"2019-06-22","weight":"0.00000000","weight_class_id":"1","length":"0.00000000","width":"3.00000000","length_class_id":"3","subtract":"1","rating":0,"reviews ":0,"sort_order":"1","viewed":"8"}]

Pojo class-

   import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    public class CategoryProductResponse{
   @SerializedName("product_id")
@Expose
private String productId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("description")
@Expose
private String description;
@SerializedName("meta_title")
@Expose
private String metaTitle;
@SerializedName("meta_description")
@Expose
private String metaDescription;
@SerializedName("meta_keyword")
@Expose
private String metaKeyword;
@SerializedName("tag")
@Expose
private String tag;
@SerializedName("model")
@Expose
private String model;
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("upc")
@Expose
private String upc;
@SerializedName("ean")
@Expose
private String ean;
@SerializedName("jan")
@Expose
private String jan;
@SerializedName("isbn")
@Expose
private String isbn;
@SerializedName("mpn")
@Expose
private String mpn;
@SerializedName("location")
@Expose
private String location;
@SerializedName("quantity")
@Expose
private String quantity;
@SerializedName("stock_status")
@Expose
private String stockStatus;
@SerializedName("image")
@Expose
private String image;
@SerializedName("manufacturer_id")
@Expose
private String manufacturerId;
@SerializedName("manufacturer")
@Expose
private String manufacturer;
@SerializedName("price")
@Expose
private String price;
@SerializedName("special")
@Expose
private Object special;
@SerializedName("reward")
@Expose
private Object reward;
@SerializedName("points")
@Expose
private String points;
@SerializedName("tax_class_id")
@Expose
private String taxClassId;
@SerializedName("date_available")
@Expose
private String dateAvailable;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("weight_class_id")
@Expose
private String weightClassId;
@SerializedName("length")
@Expose
private String length;
@SerializedName("width")
@Expose
private String width;
@SerializedName("length_class_id")
@Expose
private String lengthClassId;
@SerializedName("subtract")
@Expose
private String subtract;
@SerializedName("rating")
@Expose
private Integer rating;
@SerializedName("reviews ")
@Expose
private Integer reviews;
@SerializedName("sort_order")
@Expose
private String sortOrder;
@SerializedName("viewed")
@Expose
private String viewed;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getMetaTitle() {
return metaTitle;
}

public void setMetaTitle(String metaTitle) {
this.metaTitle = metaTitle;
}

public String getMetaDescription() {
return metaDescription;
}

public void setMetaDescription(String metaDescription) {
this.metaDescription = metaDescription;
}

public String getMetaKeyword() {
return metaKeyword;
}

public void setMetaKeyword(String metaKeyword) {
this.metaKeyword = metaKeyword;
}

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getSku() {
return sku;
}

public void setSku(String sku) {
this.sku = sku;
}

public String getUpc() {
return upc;
}

public void setUpc(String upc) {
this.upc = upc;
}

public String getEan() {
return ean;
}

public void setEan(String ean) {
this.ean = ean;
}

public String getJan() {
return jan;
}

public void setJan(String jan) {
this.jan = jan;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getMpn() {
return mpn;
}

public void setMpn(String mpn) {
this.mpn = mpn;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}

public String getStockStatus() {
return stockStatus;
}

public void setStockStatus(String stockStatus) {
this.stockStatus = stockStatus;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getManufacturerId() {
return manufacturerId;
}

public void setManufacturerId(String manufacturerId) {
this.manufacturerId = manufacturerId;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public Object getSpecial() {
return special;
}

public void setSpecial(Object special) {
this.special = special;
}

public Object getReward() {
return reward;
}

public void setReward(Object reward) {
this.reward = reward;
}

public String getPoints() {
return points;
}

public void setPoints(String points) {
this.points = points;
}

public String getTaxClassId() {
return taxClassId;
}

public void setTaxClassId(String taxClassId) {
this.taxClassId = taxClassId;
}

public String getDateAvailable() {
return dateAvailable;
}

public void setDateAvailable(String dateAvailable) {
this.dateAvailable = dateAvailable;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getWeightClassId() {
return weightClassId;
}

public void setWeightClassId(String weightClassId) {
this.weightClassId = weightClassId;
}

public String getLength() {
return length;
}

public void setLength(String length) {
this.length = length;
}

public String getWidth() {
return width;
}

public void setWidth(String width) {
this.width = width;
}

public String getLengthClassId() {
return lengthClassId;
}

public void setLengthClassId(String lengthClassId) {
this.lengthClassId = lengthClassId;
}

public String getSubtract() {
return subtract;
}

public void setSubtract(String subtract) {
this.subtract = subtract;
}

public Integer getRating() {
return rating;
}

public void setRating(Integer rating) {
this.rating = rating;
}

public Integer getReviews() {
return reviews;
}

public void setReviews(Integer reviews) {
this.reviews = reviews;
}

public String getSortOrder() {
return sortOrder;
}

public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}

public String getViewed() {
return viewed;
}

public void setViewed(String viewed) {
this.viewed = viewed;
}

}

错误- com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $[0]

编辑: 截图: Screenshot of Postman 和 Screenshot2 of Postman

使用它来创建你的 pojo http://www.jsonschema2pojo.org/ 因为你没有正确创建它。

您期望的响应有问题。

如果 Json 与您发布的相同,则服务应定义为:

@POST("api/product/getProductbyCategory")
Observable<List<CategoryProductResponse>> getProductListOnCategory(@Query("category_id") String categoryId);

请检查回复是否与您发布的完全一致,因为看起来 GSON 期待 Object,而不是 Array

编辑

您的 POJO 中有一些错误:

...
@SerializedName("special")
@Expose
private Object special; // Change this type to String
@SerializedName("reward")
@Expose
private Object reward; // Change this type to String
...
@SerializedName("reviews ") // Remove the whitespace after reviews
@Expose
private Integer reviews;

编辑 2

我在屏幕截图中注意到您没有在邮递员中将参数作为查询发送。

如果您想以与在 Postman 中相同的方式发送 category_id,您必须执行以下操作:

  1. 创建 class 发送为 body:
class ProductsRequest {

    @SerializedName("category_id")
    String categoryId;

    public ProductsRequest(String categoryId) {
        this.categoryId = categoryId;
    }
}

然后修改您的服务以使用此 class 作为调用的 body

@POST("api/product/getProductbyCategory")
    Observable<List<CategoryProductResponse>> getProductListOnCategory(@Body ProductsRequest request);

当后端期望 body.

时,您将 category_id 作为查询发送

如果响应如您所愿,您的代码将正常工作。比如说,如果您有 return 产品的正确类别 ID。 但是,如果您的类别 ID 错误并且您向服务器发送请求,它可能不是 return 一个对象数组,而是 return 一个带有错误消息的对象,或者它可能 return 什么都没有根本。在那种情况下,Gson 无法将响应附加到 CategoryProductResponseList。 名字 CategoryProductResponse 不是一个好名字。更好的名称是 CategoryProduct 或只是 Product.

我想提的另一件事是,当您没有 post 值时,为什么要使用 POST 方法来获取它? Query 参数也可以在 GET 请求中使用。