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

I am not retrieving any JSON data from treehouse blog..

I am having trouble retrieving data from the blog url. I tried logging the data after setting it to the String variable, but it didn't log anything. I don't understand what I am doing wrong.

protected String doInBackground(Object... arg0) {
        int responseCode = -1;
        try {
            URL blogFeedUrl = new URL(
                    "http://blog.teamtreehouse.com/api/get_recent_summary/?count=20");
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl
                    .openConnection();
            connection.connect();
            responseCode = connection.getResponseCode();
            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);
                JSONObject response = new JSONObject(responseData);
                String status = response.getString("status");
                Log.v(TAG,status);

                JSONArray jsonPosts = response.getJSONArray("posts");
                for(int i = 0; i < jsonPosts.length(); i ++){
                    JSONObject jsonPost = jsonPosts.getJSONObject(i);
                    System.out.println(jsonPost.get("title"));
                }

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Adding an answer here for posterity: Shirwa cleaned and rebuilt his project and it worked for him. We were unable to determine why it wasn't working prior to that.

I am not retrieving any JSON data from the treehouse blog either. I thought that I may have entered something wrong so I went over the tutorial repeatedly, yet everything was done correctly. Yet, still no JSON. I downloaded the course files and imported them into eclipse so that I could dissect them and figure out what I did wrong. However, with the downloaded files, I still did not retrieve any JSON data when I ran the app.

How do I fix this problem?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Step 1 is to take the URL from your code and plug it in your browser to see what you get. You want to make sure you can get data from your computer: http://blog.teamtreehouse.com/api/get_recent_summary/?count=20

Assuming that works, then you'll need to start debugging. Insert some breakpoints when doInBackground is called and step through the code. Maybe write some stuff to the log. Also, feel free to post your Activity code in here for us to take a look at. :smile:

Ok. Thanks. I inserted some breakpoints and then began the debugging process. My Class File Editor says that there is a source not found for the ClassLoader.class. I do not understand.

Ben. Thanks. You were right, As Always. It works now. but I still don't fully understand, but I will with time, I hope.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Cool! So glad it's working. The debugger will try to step through everything, which is why you got the ClassLoader message. Those types of messages are safe to ignore. You just need to step past that point somehow or insert a new breakpoint later on and run until there.