1

The error occurs whenever i request for @Post method. @Get method will work fine. well the response coming fine in PostMan too. Can someone help me why it is happening. Headers used are header("Content-Type", "application/json") header("Accept", "application/json")

My post request method-

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

Response I'm getting in 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;
}

}

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

EDITS: Screenshots: Screenshot of Postman and Screenshot2 of Postman

ravi
  • 899
  • 8
  • 31
Emulator
  • 45
  • 10

3 Answers3

0

use this to create your pojo http://www.jsonschema2pojo.org/ because you didn't create it correctly.

Mina George
  • 184
  • 12
0

There is something wrong with the response you are expecting.

If the Json is the same you posted, the service should be defined as:

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

Please, check the response is exactly as you posted cause it looks GSON is expecting an Object, not an Array.

EDIT

You have some errors in your 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;

EDIT 2

I noticed in the screenshot that you are not sending the param as a Query in postman.

If you want to send the category_id in the same way you do in Postman, you have to do the following:

  1. Create a class to send as body:
class ProductsRequest {

    @SerializedName("category_id")
    String categoryId;

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

Then modify your service to use this class as the body of the call:

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

You are sending the category_id as a query when the backend is expecting a body.

Jaime Suarez
  • 660
  • 4
  • 11
0

Your code will work fine if the response comes out as you have expected it to. Like lets say if you have the correct category id that returns the products. But if your category id is wrong and you send a request to the server it may not return an array of objects, instead it may return an object with error message or it may return nothing at all. In that case Gson cannot attach the response to the List of CategoryProductResponse. The name CategoryProductResponse is not a good name for it. Better name would be CategoryProduct or just Product.

The other thing I want to mention is why are you using POST method to get this when you dont have post values? The Query parameter can be used in a GET request too.

ravi
  • 899
  • 8
  • 31
  • i need to get the products for particular category_id. category_id is correct I'm getting the prooducts for the particular category_id in Postman – Emulator Jul 11 '19 at 10:58
  • Are there any other things that you might be missing ? Like header information on the android part that is used on the POSTMAN client. The problem here is what I mentioned. Due to some reason, when you are making the REST call, your data is not an array of object. Can you also attach the POSTMAN screenshot showing request body, response body and another screenshot showing header info – ravi Jul 11 '19 at 11:12
  • Attached the images- refer screenshot-2 and screenshot-3 – Emulator Jul 11 '19 at 11:51