Skip to main content

Retrive JsonObject inside JsonArray using Retrofit in android


my json is shown below:
  {
"status": "ok",
"count": 1,
"pages": 1,
"category": {
    "id": 44,
    "slug": "flex-print",
    "title": "Flex Print",
    "description": "Flex Print",
    "parent": 7,
    "post_count": 1
},
"posts": [
    {
        "id": 343,
        "type": "post",
        "slug": "nk-advertising",
        "url": "https://btownmedia.com/2018/10/10/nk-advertising/",
        "status": "publish",
        "title": "NK Advertising",
        "title_plain": "NK Advertising",
        "content": "<p style=\"color: #7a6666; font-family: roboto-regular-webfont; font-size: 14px; line-height: 24px; margin: 0px; padding: 0px 0px 19px; width: 370px;\"><strong>Online advertisement:</strong> <em>Online advertisement on our website</em> means our basic service. Your business card or a visiting card sized advertisement is displayed on our website for pre-mentioned time frame.</p>\n<p style=\"color: #7a6666; font-family: roboto-regular-webfont; font-size: 14px; line-height: 24px; margin: 0px; padding: 0px 0px 19px; width: 370px;\"><strong>Personalized page:</strong> We offer you a personalized page, designed for your business with all the details of your business- linked with your advertisement that opens when clicked.</p>\n",
        "excerpt": "<p>Online advertisement on our website means our basic service. Your business card or a visiting card sized advertisement is displayed on our website for pre-mentioned time frame.</p>\n",
        "date": "2018-10-10 08:08:29",
        "modified": "2018-10-10 08:08:29",
        "categories": [
            {
                "id": 44,
                "slug": "flex-print",
                "title": "Flex Print",
                "description": "Flex Print",
                "parent": 7,
                "post_count": 1
            }
        ],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "admin",
            "name": "admin",
            "first_name": "",
            "last_name": "",
            "nickname": "admin",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [
            {
                "id": 342,
                "url": "https://btownmedia.com/nk/",
                "slug": "nk",
                "title": "nk",
                "description": "",
                "caption": "",
                "parent": 343,
                "mime_type": "image/png",
                "images": {
                    "full": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "thumbnail": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                        "width": 150,
                        "height": 142
                    },
                    "medium": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "medium_large": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "large": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "rpggallery_admin_thumb": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "rpggallery_admin_large": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "rpg_gallery_admin_thumb": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                        "width": 150,
                        "height": 142
                    },
                    "rpg_gallery_thumb": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                        "width": 150,
                        "height": 142
                    },
                    "estore-featured-image": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "estore-product-grid": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-75x75.png",
                        "width": 75,
                        "height": 75
                    },
                    "estore-square": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "estore-slider": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    },
                    "estore-medium-image": {
                        "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                        "width": 230,
                        "height": 142
                    }
                }
            }
        ],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "thumbnail": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                "width": 150,
                "height": 142
            },
            "medium": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "medium_large": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "large": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "rpggallery_admin_thumb": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "rpggallery_admin_large": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "rpg_gallery_admin_thumb": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                "width": 150,
                "height": 142
            },
            "rpg_gallery_thumb": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-150x142.png",
                "width": 150,
                "height": 142
            },
            "estore-featured-image": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "estore-product-grid": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk-75x75.png",
                "width": 75,
                "height": 75
            },
            "estore-square": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "estore-slider": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            },
            "estore-medium-image": {
                "url": "https://btownmedia.com/wp-content/uploads/2018/10/nk.png",
                "width": 230,
                "height": 142
            }
        }
    }
]} 
Post.java
public class Post {
private Integer id;
private String type;
private String slug;
private String url;
private String status;
private String title;
private String titlePlain;
private  String content;
private  String excerpt;
private String thumbnail;


public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getSlug() {
    return slug;
}

public void setSlug(String slug) {
    this.slug = slug;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getTitlePlain() {
    return titlePlain;
}

public void setTitlePlain(String titlePlain) {
    this.titlePlain = titlePlain;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getExcerpt() {
    return excerpt;
}

public void setExcerpt(String excerpt) {
    this.excerpt = excerpt;
}

public String getThumbnail() {
    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}
}
PostArtical.java
public class PostArtical {
private String status;
private Integer count;
private Integer pages;
private Category category;
private List<Post> posts = null;


public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Integer getCount() {
    return count;
}

public void setCount(Integer count) {
    this.count = count;
}

public Integer getPages() {
    return pages;
}

public void setPages(Integer pages) {
    this.pages = pages;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public List<Post> getPosts() {
    return posts;
}

public void setPosts(List<Post> posts) {
    this.posts = posts;
}
}
Category.java
public class Category {

private Integer id;
private String slug;
private String title;
private String description;
private Integer parent;
private Integer postCount;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getSlug() {
    return slug;
}

public void setSlug(String slug) {
    this.slug = slug;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Integer getParent() {
    return parent;
}

public void setParent(Integer parent) {
    this.parent = parent;
}

public Integer getPostCount() {
    return postCount;
}

public void setPostCount(Integer postCount) {
    this.postCount = postCount;
}
}
MainActivity.java
//inside the onCreate method
  onCreate method   {     getRetrofitPostObject();}
//out side the onCreate Method
 void getRetrofitPostObject() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(urlp)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);

    Call<PostArtical> call = service.getPostDetails();

    call.enqueue(new Callback<PostArtical>() {
        @Override
        public void onResponse(Response<PostArtical> response, Retrofit retrofit) {

            try {
                    Log.d("Category", response.body().getCategory().getTitle());

                Log.d("PostText", response.body().getPosts().get(0).getTitle());

                List<Post> PostData = response.body().getPosts();



                for (int i = 0; i < PostData.size(); i++) {

                    if (i == 0) {
                        text_id_1.setText("id  :  " + PostData.get(i).getId());
                        Log.d("Atrical[0]", String.valueOf(PostData.get(i).getId()));
                        Log.d("Atrical[title]", String.valueOf(PostData.get(i).getTitle()));
                        Log.d("Atrical[content]", String.valueOf(PostData.get(i).getContent()).replaceAll("(<[^>]+>)|(&[^;]+;)",""));

                        Log.d("Atrical[Excerpt]", String.valueOf(PostData.get(i).getExcerpt()).replaceAll("(<[^>]+>)|(&[^;]+;)",""));
                        Log.d("Atrical[image]", String.valueOf(PostData.get(i).getThumbnail()));
                        img = PostData.get(i).getThumbnail();

                       DownloadImageWithURLTask downloadTask = new DownloadImageWithURLTask(imageview);
                        downloadTask.execute(img);



                        // text_name_1.setText("title  :  " + StudentData.get(i).getTitle());

                    }
                }


              //  text_id_1.setText("PostStatus  :  " + response.body().getStatus());
                text_name_1.setText("PostCount  :  " + response.body().getCount());
                text_marks_1.setText("PostPage  : " + response.body().getPages());

            } catch (Exception e) {
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("onFailure", t.toString());
        }
    });}

//out side the onCreate Method Download image: 
private class DownloadImageWithURLTask extends AsyncTask<String, Void, Bitmap> {

    ImageView bmImage;

    public DownloadImageWithURLTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String pathToFile = urls[0];
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(pathToFile).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap result) {


        bmImage.setImageBitmap(result);

    }

}

Comments

Popular posts from this blog

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...

Circular Button with Icon and Text in Android

Circular Button with Icon and Text in Android Rounded android button can be used for many purposes to make awesome application. We can see many applications that have used circle button or rounded corner. In this tutorial, I am going to show how to make circular android button and to add icons/images and text in the same button. To make circular button in android app, you don’t need any java code, this can be done by using only XML. Rounded button can also be created by using java code but it is time consuming and need to have advance knowledge of java programming. Here you will learn to make circular/rounded corner button using XML only. Related: Android Button with Icon and Text Adding Badge (Item Count) to Android Button Android Switch Button Example Android Example: How to Create Circular Button with Icon and Text in Android First you have to create a new XML file in drawable folder to make rounded button. In this file I have made a rectangle and gave border radius to ma...