I have images under array...here is my json:
{
"status": 200,
"data": {
"id": 1,
"product_category_id": 1,
"name": "Centre Coffee Table",
"producer": "Luna",
"description": "Mild Steel Base In Poder Coated White Finish.8 mm Tempered Glass Table Top.Bottom Shelf In Paimted Brown Glass.",
"cost": 5000,
"rating": 3,
"view_count": 21243,
"created": "2015-09-07T09:24:05+0000",
"modified": "2020-07-27T10:52:42+0000",
"product_images": [
{
"id": 1,
"product_id": 1,
"image": "http://staging.php-dev.in:8844/trainingapp/uploads/prod_img/thumb/medium/9dc6234da018916e545011fa1.jpeg",
"created": "2015-09-07T09:40:00+0000",
"modified": "2015-09-07T09:40:00+0000"
},
{
"id": 6,
"product_id": 1,
"image": "http://staging.php-dev.in:8844/trainingapp/uploads/prod_img/thumb/medium/1bfdac02ced672dd1e8e8976c.jpeg",
"created": "2015-09-07T09:44:11+0000",
"modified": "2015-09-07T09:44:11+0000"
}
]
}
}
From the above json you'll see there are two images under product_images
array
I will tell you images under product_images
are not fixed ...it could be differnt ids..can be contain 3 or 2 or 1 or 4
Here is my second json response where id 3 ids with 3 images are retrive under array-->
{
"status": 200,
"data": {
"id": 5,
"product_category_id": 2,
"name": "HP Fabric Office Chair",
"producer": "HP",
"description": "Serene Staff Chair
Five Star Nylon Base with Nylon Castor
Free Delivery",
"cost": 2222,
"rating": 3,
"view_count": 2335,
"created": "2015-09-07T09:48:51+0000",
"modified": "2020-07-27T11:50:51+0000",
"product_images": [
{
"id": 10,
"product_id": 5,
"image": "http://staging.php-dev.in:8844/trainingapp/uploads/prod_img/thumb/medium/fddd92b395be88ce117936b9f.jpeg",
"created": "2015-09-07T09:49:33+0000",
"modified": "2015-09-07T09:49:33+0000"
},
{
"id": 11,
"product_id": 5,
"image": "http://staging.php-dev.in:8844/trainingapp/uploads/prod_img/thumb/medium/78b1d4e168280fe5f9cfdff60.jpeg",
"created": "2015-09-07T09:49:51+0000",
"modified": "2015-09-07T09:49:51+0000"
},
{
"id": 12,
"product_id": 5,
"image": "http://staging.php-dev.in:8844/trainingapp/uploads/prod_img/thumb/medium/b6480396b91bb9e99e40e4ba7.jpeg",
"created": "2015-09-07T09:50:10+0000",
"modified": "2015-09-07T09:50:10+0000"
}
]
}
}
Here is my retrofit activity:
RetrofitClient.instancetable.fetchUserdetail(id)
.enqueue(object : Callback<Product_base_response> {
override fun onFailure(call: Call<Product_base_response>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<Product_base_response>,
response: Response<Product_base_response>
) {
var res = response
val ret: Product_Data_response? =res.body()?.data
val retro: List<Product_images_response> = res.body()?.data?.product_images!!
Log.e("checkdata",ret?.name.toString())
val ygd=ret?.name.toString()
text.setText(ygd)
text2.setText(ret?.producer.toString())
//need help in here -->>>>
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(0).image).into(imagemain);
for ( i in res?.body()!!.data.product_images.indices.toString()){//crashes if contain image under get(1)
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(1).image).into(imagemain1);
}
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(2).image).into(imagemain2);
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(3).image).into(imagemain3);
}
})
}
From above retrofit response code it got crashes when it not deteected get(1) in image array
NOTE: I have to load those images in 4 imageviews and also note images are not fixed as you can see json 1 and json 2.
Thanks in advance for help.
See Question&Answers more detail:
os