[英]Facing issue in API response: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY
I am using retrofit rxjava to get data from server.我正在使用 retrofit rxjava 从服务器获取数据。 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.
我的 API 响应包含产品列表,并且它具有缩略图选项,但是当没有缩略图 API 返回 null 数组而不是字符串时会出现问题。 So, I get error like "java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 12 column 21 path $.result[0].thumbnail"
因此,我收到类似“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这是我的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"
}
]
}
Change data type of thumbnail to Any like thumbnail: Any?将缩略图的数据类型更改为 Any like thumbnail: Any? Then acess the variable by checking the type
然后通过检查类型来访问变量
if(thumbnail is String) {
//set the image
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.