Facing issue in API response: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY

Viewed 43

I am using retrofit rxjava to get data from server. My API reponse contains a list of products and it has option of thumbnails but issue occurse when there is no thumbnail API returns null arrayand not the string. So, I get error like "java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 12 column 21 path $.result[0].thumbnail"

How can I solve this can anyone suggest me.

this is my pojo class

class ResultPojo {
    @SerializedName("name")
    @Expose
    var name: String? = null

    @SerializedName("link")
    @Expose
    var link: String? = null

    @SerializedName("current_price")
    @Expose
    var currentPrice: Int? = null

    @SerializedName("original_price")
    @Expose
    var originalPrice: Int? = null

    @SerializedName("discounted")
    @Expose
    var discounted: Boolean? = null

    @SerializedName("thumbnail")
    @Expose
    var thumbnail: String? = null


    @SerializedName("query_url")
    @Expose
    var queryUrl: String? = null
} 

here is my reponse from server

{
    "total_result": 40,
    "query": "electronics",
    "fetch_from": "https://www.flipkart.com/search?marketplace=FLIPKART&q=electronics",
    "result": [
        {
            "name": "Dr. Morepen GLUCO ONE BG-03 50 STRIP PACK OF 1 50 Gluco...",
            "link": "https://www.flipkart.com/dr-morepen-gluco-one-bg-03-50-strip-pack-1-glucometer-strips/p/itm50b818a05c5ef",
            "current_price": 617,
            "original_price": 849,
            "discounted": true,
            "thumbnail": [],
            "query_url": "https://flipkart.dvishal485.workers.dev/product/dr-morepen-gluco-one-bg-03-50-strip-pack-1-glucometer-strips/p/itm50b818a05c5ef"
        },
        {
            "name": "PHILIPS BT1232/15 Trimmer 30 mins  Runtime 3 Length Set...",
            "link": "https://www.flipkart.com/philips-bt1232-15-trimmer-30-mins-runtime-3-length-settings/p/itm52bc527efdd34",
            "current_price": 849,
            "original_price": 995,
            "discounted": true,
            "thumbnail": [],
            "query_url": "https://flipkart.dvishal485.workers.dev/product/philips-bt1232-15-trimmer-30-mins-runtime-3-length-settings/p/itm52bc527efdd34"
        },
        {
            "name": "Butterfly Rapid Kettle 1.5 Litre + Eco 750 Ml Water Bot...",
            "link": "https://www.flipkart.com/butterfly-rapid-kettle-1-5-litre-eco-750-ml-water-bottle/p/itmf3c7b5f9a57e6",
            "current_price": 693,
            "original_price": 1499,
            "discounted": true,
            "thumbnail": [],
            "query_url": "https://flipkart.dvishal485.workers.dev/product/butterfly-rapid-kettle-1-5-litre-eco-750-ml-water-bottle/p/itmf3c7b5f9a57e6"
        },
        {
            "name": "Whirlpool 7 kg Magic Clean 5 Star Semi Automatic Top Lo...",
            "link": "https://www.flipkart.com/whirlpool-7-kg-magic-clean-5-star-semi-automatic-top-load-black-grey/p/itmbb7baa1714ffb",
            "current_price": 10190,
            "original_price": 13600,
            "discounted": true,
            "thumbnail": [],
            "query_url": "https://flipkart.dvishal485.workers.dev/product/whirlpool-7-kg-magic-clean-5-star-semi-automatic-top-load-black-grey/p/itmbb7baa1714ffb"
        },
        {
            "name": "Orient Electric by orient Fabri Press DIFP10BP 1000 W D...",
            "link": "https://www.flipkart.com/orient-electric-fabri-press-difp10bp-1000-w-dry-iron/p/itm74bc3b21a2eb2",
            "current_price": 599,
            "original_price": 990,
            "discounted": true,
            "thumbnail": [],
            "query_url": "https://flipkart.dvishal485.workers.dev/product/orient-electric-fabri-press-difp10bp-1000-w-dry-iron/p/itm74bc3b21a2eb2"
        },
        {
            "name": "Butterfly Rapid Plus Induction Cooktop",
            "link": "https://www.flipkart.com/butterfly-rapid-plus-induction-cooktop/p/itm07b182c8a7745",
            "current_price": 1250,
            "original_price": 3399,
            "discounted": true,
            "thumbnail": "https://rukminim1.flixcart.com/image/612/612/kvsfhjk0/induction-cook-top/w/1/0/rapid-plus-tripoh0085-butterfly-original-imag8myagqjfdhcc.jpeg?q=70",
            "query_url": "https://flipkart.dvishal485.workers.dev/product/butterfly-rapid-plus-induction-cooktop/p/itm07b182c8a7745"
        },
        {
            "name": "NOVA NHP 8100/05 Hair Dryer",
            "link": "https://www.flipkart.com/nova-nhp-8100-05-hair-dryer/p/itm985b1f3c8f641",
            "current_price": 299,
            "original_price": 845,
            "discounted": true,
            "thumbnail": "https://rukminim1.flixcart.com/image/612/612/ktn9pjk0/hair-dryer/n/l/0/silky-shine-hot-and-cold-foldable-nhp-8100-05-nova-original-imag6y7wdgzhc3nh.jpeg?q=70",
            "query_url": "https://flipkart.dvishal485.workers.dev/product/nova-nhp-8100-05-hair-dryer/p/itm985b1f3c8f641"
        }
    ]
}
1 Answers

Change data type of thumbnail to Any like thumbnail : Any? Then acess the variable by checking the type

    if(thumbnail is String) {
    //set the image
     }
Related