Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


JSON Api on my blog

I have installed the JSON api plug in in my blog and added the code from github to the core.php, and then edited the blogfeedURL to http://http://eli5.me/api/get_recent_summary/?count=10

I am not getting anything when I run the emulator. It says no items to display so I am guessing it didn't work. I am really confused as I have read a lot of solutions in the forums for this but it's really confusing to me as I am not a coder or anything and I have really low knowledge about editing codes that I didn't write and didn't learn. So anyone can teach me step by step. It's a wordpress blog..

Edit: ok so I found the API link that I should add to the app and it is https://public-api.wordpress.com/rest/v1/sites/www.eli5.me/posts/

it looks different than the one treehouse looks like, so I am guessing I have to edit some codes on my app for it to show the blog posts normally. Anyone has an idea what to edit?

1 Answer

Ben Jakuben
Ben Jakuben
Treehouse Teacher

The data format is obviously different, but it is similar. What you'll need to do is go one step at a time and make sure each part is working.

Your code inside the handleBlogResponse() method is probably similar to the following (I didn't include the whole method):

        JSONArray jsonPosts = mBlogData.getJSONArray("posts");
        ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
        for (int i = 0;i < jsonPosts.length(); i++) {
            JSONObject post = jsonPosts.getJSONObject(i);
            String title = post.getString(KEY_TITLE);
            title = Html.fromHtml(title).toString();
            String author = post.getString(KEY_AUTHOR);
            author = Html.fromHtml(author).toString();

            HashMap<String, String> blogPost = new HashMap<String, String>();
            blogPost.put(KEY_TITLE, title);
            blogPost.put(KEY_AUTHOR, author);


        String[] keys = {KEY_TITLE, KEY_AUTHOR};
        int[] ids = {android.R.id.text1, android.R.id.text2};
        SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_list_item_1, keys, ids);

You want to make sure each line works, so you'll need to either step through with the debugger or add log statements to peek at the data being worked with.

The first step is to make sure that jsonPosts is getting set. It should work since your blog has the same array of "posts" as the Treehouse blog. Most of the fields and the structure are the same, so it should hopefully mostly work.

Next, in the for loop, make sure post gets set, and then the title should work as well because again, the format is the same.

author is different in your JSON data, though:

author: {
  ID: 1,
  email: false,
  name: "qaximor",
  URL: "",
  avatar_URL: "http://1.gravatar.com/avatar/985e014bee2414eebb016945d0e1057c?s=96&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&r=G",
  profile_URL: "http://en.gravatar.com/985e014bee2414eebb016945d0e1057c"

It's a whole JSON object, so you'd need to create a JSONObject and then extract the name. Here is some untested code you could start with:

JSONObject authorObject = post.getJSONObject("author");
String author = authorObject.getString("name");
author = Html.fromHtml(author).toString();

Hopefully this gets you moving. If you run into more trouble, post it in a new thread in the Forum. Good luck!

Thanks! It worked :D