0

api url: "http://btownmedia.com/?json=get_category_posts&slug=flex-print&status=publish"

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

Pojo Class:

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 date;
private String modified;
private List<Object> tags = null;
private List<Object> comments = null;
private Integer commentCount;
private String commentStatus;
private String thumbnail;
private String thumbnailSize;

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 getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getModified() {
    return modified;
}

public void setModified(String modified) {
    this.modified = modified;
}



public List<Object> getTags() {
    return tags;
}

public void setTags(List<Object> tags) {
    this.tags = tags;
}



public List<Object> getComments() {
    return comments;
}

public void setComments(List<Object> comments) {
    this.comments = comments;
}



public Integer getCommentCount() {
    return commentCount;
}

public void setCommentCount(Integer commentCount) {
    this.commentCount = commentCount;
}

public String getCommentStatus() {
    return commentStatus;
}

public void setCommentStatus(String commentStatus) {
    this.commentStatus = commentStatus;
}

public String getThumbnail() {
    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}



public String getThumbnailSize() {
    return thumbnailSize;
}

public void setThumbnailSize(String thumbnailSize) {
    this.thumbnailSize = thumbnailSize;
}

}

interfaceAPI

@GET("json=get_category_posts&slug=flex-print&status=publish")
Call<Post> getListPost();

MainActivity.java

void getListPosts() {

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

    RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
    Log.d("service", String.valueOf(""));

    Call<Post> call = service.getListPost();

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

            try {

                Log.d("Title", String.valueOf(response.body()));


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

        }

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

How to fetch "posts" array in json? I have tryed as shown in above program. can any one please help me out. In the above program all, I wanted to fetch only posts array in android.

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

]

TommySM
  • 3,793
  • 3
  • 24
  • 36
  • your JSON seems malformed, which will make it hard for ant parser to do it's job, fix the JSON first. – TommySM Oct 22 '18 at 14:34

2 Answers2

0

The simplest solution would be to create an upper class for the whole object that your api returns and then get your posts from it?

Something like:

class APIResponse {
    public List<Post> posts;

    // add the other fields from your response if you need them
}

then you just have to replace your call by

@GET("json=get_category_posts&slug=flex-print&status=publish")
Call<APIResponse> getListPost();

you will then be able to simply access the list of posts easily like response.posts

However, if you don't want to have an upper class but directly the list of posts, you might need to use a custom deserializer. Something like this:

public static class PostAdapter implements JsonDeserializer<List<Post>> {
    public List<Post> deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext ctx) {
        JsonObject json = jsonElement.getAsJsonObject();
        List<Post> vals = new ArrayList<>();
        if (json.has("posts") && .json.get("posts").isJsonArray()) {
            for (JsonElement e : json.get("posts").getAsJsonArray()) {
                vals.add((Post) ctx.deserialize(e, Post.class));
            }
        } 
        return vals;
    }
}

that you can register to your gson builder when creating the retrofit service:

Type postListType = new TypeToken<List<Post>>() {}.getType();

Gson gson = new GsonBuilder()
        .registerTypeAdapter(postListType, new PostAdapter())
        .create();
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url1)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

by doing this, every time you'll declare in your interfaceAPI a Call> it will use this adapter to get the correct results. So now you can do

@GET("json=get_category_posts&slug=flex-print&status=publish")
Call<List<Post>> getListPost();
Macmist
  • 713
  • 3
  • 11
0

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