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

Android

Getting JSON from Blogger blog

I'm guessing this is a fairly dumb question but I'm new to Android and Java.

I'd like to adapt the BlogReader I've developed by working through Ben's course to read my own blog, which has an xml feed at http://friendly-encounters.blogspot.co.uk/feeds/posts/default?alt=rss

Google's code playground at http://code.google.com/apis/ajax/playground/#list_posts shows me how to this it with Javascript and it works. But how do I get it to work in Ben's Java BlogReader?

If I just pop the location of my feed into Ben's code as follows: int responseCode = -1; JSONObject jsonResponse = null;

        try {
            URL blogFeedURL = new URL("http://friendly-encounters.blogspot.co.uk/feeds/posts/default?alt=rss");
            HttpURLConnection connection = (HttpURLConnection) blogFeedURL.openConnection(); 
            connection.connect();

I get a message that says "Oops Sorry! Error getting data from the blog" presumably because it wants JSON data and it's getting xml.

What is the simplest solution please? Can I generate JSON from within Blogger? Or do I need to program in my BlogReader the Java equivalent of the Javascript that works in Google code playground?

6 Answers

Thanks but these links are all about Javascript. My question was how to handle an xml feed within an Android application in Java that wants a JSON feed. Or alternatively how to generate a JSON feed from Blogger rather than an xml feed.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Looks like you can request JSON data from your blog pretty easily! I just changed the URL to ask for JSON instead: http://friendly-encounters.blogspot.co.uk/feeds/posts/default?alt=json

The JSON output by that is pretty complex, but you have a "feed" object that has an array of posts called "entry". From there you can get the title, link, etc.

If you try this, step one will be to see if, using this version of the URL, you can get your app to connect and write the JSON data to the log or something.

Step 2 will then be to figure out how to properly parse the JSON data to get down to the entries you want. Then you can hopefully adapt the data the same way we're using a SimpleAdapter for a HashMap of values in the BlogReader project.

If you get stuck at any point, search for your errors on Google or in the Forum here and post follow up questions as new posts. Hopefully it's all pretty straightforward from here, but we're here to help if you get stuck!

That's just the information I needed Ben, thank you. I didn't know Blogger blogs produced a JSON feed.

Now all I need to do is work through your video Parsing Data Returned in JSON, trying the various steps with my own blog to see what works.. In fact I'll probably try it first on a smaller test blog, so I can more easily see the structure of the JSON feed. The Firefox add-on called JSONView is useful for that.

Sounds straightforward now you've given me the info I needed. Thank you.

Not as straightforwards as I thought. Here's the problem.

The JSON from Ben's example looks like this: "posts": [

{
    "id": 22855,
    "url": "http://blog.teamtreehouse.com/psd-to-html-is-dead",
    "title": "PSD to HTML is Dead",
    "date": "2014-01-14 12:00:55",
    "author": "Nick Pettit",
    "thumbnail": "http://blog.teamtreehouse.com/wp-content/uploads/2014/01/psd2html-150x150.png"
},

Which means it's nice and easy to get the string values of title, author etc.

But the JSON from my blog looks like this: "entry": [

{
    "id": {
        "$t": "tag:blogger.com,1999:blog-5986809131543189129.post-5640610698144324709"
    },
    "published": {
        "$t": "2014-01-14T13:29:00.000Z"
    },
    "updated": {
        "$t": "2014-01-14T22:28:06.965Z"
    },
    "title": {
        "type": "text",
        "$t": "Always an artist"
    }, 

I don't understand what's going on with all those $ts and I don't know how to extract the strings I'm interested in, such as "Always an artist".

Help please?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Those are just the key names...sounds like it might be something like this: http://stackoverflow.com/questions/20646440/what-does-a-dollar-sign-in-a-json-key-do

I would concentrate on getting the data you care about and ignoring the rest. And start small. Maybe something like this?

  1. Get the "version" from the root element.
  2. Get "feed" as a JSONObject.
  3. From the "feed" JSONObject, get the "title" as another JSONObject.
  4. From the "title" JSONObject, get the "$t" string.
  5. From the "feed" JSONObject, get the "entry" element as a JSONArray
  6. Work through the array in a manner similar to what we do in the video

And certainly stop back here with questions along the way! If they are new questions, please post them in new threads, otherwise build on where appropriate. :) Good luck!

Sounds good. Thank you.