user1780370
user1780370

Reputation: 851

How to read JSON array and display image in Android

I am developing android listing images app where I got array from server side. But I have issue to read array or don't know how to get it.

Below array i got from server API call:

{"lists":["http:\/\/xyz.com\/projects\/photo\/birthday\/1.jpg","http:\/\/xyz.com\/projects\/photo\/birthday\/3.jpg","http:\/\/xyz.com\/projects\/photo\/birthday\/4.jpg"],"Status":"1"}

How to read it and display images in loop.

Thanks

Upvotes: 0

Views: 626

Answers (1)

George Thomas
George Thomas

Reputation: 4586

If you are using gson library for parsing json respose then you could simply write a separate class like this

public class ImageResponse{
    public String Status;
    public List<String> lists;
}

NOTE: when creating a class the object names of the class should match with the tag names from the server..

Then using gson

 ImageResponse detail = (new Gson()).fromJson(
                    response.toString(), ImageResponse.class);

where response is your response from the server

now u can access the list like

List<String> images =details.lists;

Now you have all the images in the list form, you can load the images using Picasso[lets say in a listview]

Picasso.with(context).load(images[0]).into(imageView);

For using this you should add GSON library to your dependency in your app gradle

 compile 'com.google.code.gson:gson:2.2.4'

There are many other method too, feel free to search and find out may be there will be more optimized code than this..

Upvotes: 1

Related Questions