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

JSON data problem

Hi Ben, I''d appreciate your help please.

I'm getting a strange error when I try to adapt your BlogReader to read a different URL.

Everything works fine when I use the blog.teamtreehouse.com URL. But when I try the program on different URLs it throws an exception.

If I use the debugger I find the reason for the exception is that contentLength in the following code has a value of -1. The if block starts to execute however, which means responseCode is 200. This happens with any URL I've tried, other than the original Treehouse URL. For example it happens with

URL("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON"); and with my own blog: URL ("http://friendly-encounters.blogspot.co.uk/feeds/posts/default?alt=json");

BlogReader code snippet if (responseCode==HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream);

                int contentLength = connection.getContentLength();
                char [] charArray = new char[contentLength];
                reader.read(charArray);
                String responseData = new String(charArray);
                Log.v(TAG, responseData);

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

There's a bug in the code from the project that occurs on certain blogs. You can replace the code with an updated version that I worked out with some students:

https://teamtreehouse.com/forum/trying-to-retrieve-data-from-my-own-blog-help

Hope this helps!

That looks as if it will. Thank you.