Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

android - How should I fetch images under an array using retrofit

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this way...

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.

Now... change you api call method

Call<Product_base_response>

to

Call<JsonObject>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...