我应该如何使用改型在数组下获取图像 [英] How should I fetch images under an array using retrofit

查看:69
本文介绍了我应该如何使用改型在数组下获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组下有图片...这是我的json:

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"
            }
        ]
    }
}

从上面的json中,您将看到在product_images数组下有两张图片

我会告诉您product_images下的图像不是固定的...可能是不同的ID.可以包含3个或2个或1个或4个

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

这是我的第二个json响应,其中具有3个图像的id 3 ids在数组下检索->

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\r\nFive Star Nylon Base with Nylon Castor\r\nFree 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"
            }
        ]
    }
}

这是我的改装活动:

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

            }
        })
}

从上面的改造响应代码中,未检测到图像数组中的get(1)时,它便崩溃了

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

预先感谢您的帮助.

推荐答案

尝试这种方式...

首先从响应中创建模型类,然后复制响应并在此处添加响应 http://pojo .sodhanalibrary.com/,它将在您的响应中创建所有pojo类.

First off create model classes from your response, for that copy your response and add your response here http://pojo.sodhanalibrary.com/ , here it will create all pojo classes from your response.

现在... 更改您的api调用方法

Now... change you api call method

Call<Product_base_response>

Call<JsonObject>

现在只需使用onResponse方法打印响应

Now just print response in onResponse method like

Log.d("===","onResponse :: "+response.body());

如果您在这里收到答复,

if you got your response here,

   Gson gson = new Gson();
Your_Main_Model mainModel = gson.fromJson(jsonObject.toString(), Your_Main_Model.class);

现在,要获取图像,您需要通过以下主要模型进行操作

Now, to get images you need to go through main model like

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

希望这行得通,如果您听不懂或行不通,请告诉我

Hope this works, if you didn't get it or this not work let me know

这篇关于我应该如何使用改型在数组下获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆