Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我应该如何使用改型获取阵列下的图像_Android_Json_Kotlin_Retrofit_Retrofit2 - Fatal编程技术网

Android 我应该如何使用改型获取阵列下的图像

Android 我应该如何使用改型获取阵列下的图像,android,json,kotlin,retrofit,retrofit2,Android,Json,Kotlin,Retrofit,Retrofit2,我在数组下有图像…这是我的json: { "status": 200, "data": { "id": 1, "product_category_id": 1, "name": "Centre Coffee Table", "producer": "Luna"

我在数组下有图像…这是我的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"
            }
        ]
    }
}
从上面的json中,您将看到
product\u images
array下有两个图像 我将告诉您,
产品下的图像\u图像
不是固定的…它可能是不同的ID..可以包含3或2或1或4

这是我的第二个json响应,其中id 3 id和3个图像在数组-->

以下是我的改装活动:

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);

            }
        })
}
client.instancetable.fetchUserdetail(id)
.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“res”和“+t”)
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
var res=响应
val ret:Product_Data_response?=res.body()?.Data
val retro:List=res.body()?.data?.product\u image!!
Log.e(“checkdata”,ret?.name.toString())
val ygd=ret?.name.toString()
text.setText(ygd)
text2.setText(ret?.producer.toString())
//这里需要帮助-->>>>
将(res?.body()!!.data.product_images.get(0.image))加载到(imagemain)中;
对于(i in res?.body()!!.data.product_images.index.toString()){//如果get(1)下包含图像,则崩溃
使用(getApplicationContext()).load(res?.body()!!.data.product_images.get(1.image)).into(imagemain1);
}
使用(getApplicationContext())将(res?.body()!!.data.product_images.get(2.image))加载到(imagemain2)中;
使用(getApplicationContext())将(res?.body()!!.data.product_images.get(3.image))加载到(imagemain3)中;
}
})
}
从上面的改装响应代码中,当未在图像阵列中检测到get(1)时,它崩溃了

注意:我必须在4个ImageView中加载这些图像,并且注意图像不是固定的,因为您可以看到json 1和json 2

提前感谢您的帮助。

试试这个方法

首先,从您的响应创建模型类,为此,复制您的响应并将您的响应添加到这里,在这里它将从您的响应创建所有pojo类

现在。。。 更改api调用方法

Call<Product_base_response>
如果你在这里得到回应

   Gson gson = new Gson();
Your_Main_Model mainModel = gson.fromJson(jsonObject.toString(), Your_Main_Model.class);
现在,要获得图像,您需要浏览主模型,如

Main_model.data_object_model.product_images_model_list.get(0).getImage();

希望这能起作用,如果您没有得到它或这不起作用,请让我知道

为什么您要做循环,但总是得到
1
?请参见
产品图片
数组…带有ID的图片不是固定的…它可以包含一个对象(一个图片)或最多可以包含四个对象(四个图片)..我试图将其置于循环之下,就像根据
product\u image
数组下对象的大小一样….如果使用循环遍历数组,但硬编码位置
1
循环的点是什么,您是否考虑过如何处理@cutiko<代码>加载(res?.body()!!.data.product\u images.get(1).image)->
product\u images.get(i).image
?其中
i
是循环上的索引,如果我执行
product_images.get(i)。image
它在imagemain1(get(1))中显示图像……但是对于第二个图像,它在加载到imagemain2(即get(2))时崩溃,如果它只包含imagemain not get(1)imagemain1@cutiko的get(0)图像,它也会失败/崩溃
Log.d("===","onResponse :: "+response.body());
   Gson gson = new Gson();
Your_Main_Model mainModel = gson.fromJson(jsonObject.toString(), Your_Main_Model.class);
Main_model.data_object_model.product_images_model_list.get(0).getImage();