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

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

问题描述

我在数组下有图像...这是我的 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 id 在数组下检索-->

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

这是我的改造活动:

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天全站免登陆