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 Build a Blog Reader Android App Getting Data from the Web Parsing Data Returned in JSON Format

Kevin Moslehpour
Kevin Moslehpour
4,119 Points

Bug in the reader method...Please advise

I am using android studio to program this app and discovered the reader that reads in the inputStream is converting the data incorrectly for some reason. When I go into debug mode and check my item count and pages, they are displaying the incorrect results.

here is my debug window for the content length: reader = {java.io.InputStreamReader@831727394904} contentLength = 3034

Here is my CharArray debug window (you can see the count being displayed 10 instead of 20, same goes for the pages): charArray = {char[3034]@831727410376} [0] = '{' 123 [1] = '"' 34 [2] = 's' 115 [3] = 't' 116 [4] = 'a' 97 [5] = 't' 116 [6] = 'u' 117 [7] = 's' 115 [8] = '"' 34 [9] = ':' 58 [10] = '"' 34 [11] = 'o' 111 [12] = 'k' 107 [13] = '"' 34 [14] = ',' 44 [15] = '"' 34 [16] = 'c' 99 [17] = 'o' 111 [18] = 'u' 117 [19] = 'n' 110 [20] = 't' 116 [21] = '"' 34 [22] = ':' 58 [23] = '1' 49 [24] = '0' 48 [25] = ',' 44 [26] = '"' 34 [27] = 'c' 99 [28] = 'o' 111 [29] = 'u' 117 [30] = 'n' 110 [31] = 't' 116 [32] = '_' 95 [33] = 't' 116 [34] = 'o' 111 [35] = 't' 116 [36] = 'a' 97 [37] = 'l' 108 [38] = '"' 34 [39] = ':' 58 [40] = '1' 49 [41] = '8' 56 [42] = '4' 52 [43] = '7' 55 [44] = ',' 44 [45] = '"' 34 [46] = 'p' 112 [47] = 'a' 97 [48] = 'g' 103 [49] = 'e' 101 [50] = 's' 115 [51] = '"' 34 [52] = ':' 58 [53] = '1' 49 [54] = '8' 56 [55] = '5' 53 [56] = ',' 44 [57] = '"' 34 [58] = 'p' 112 [59] = 'o' 111 [60] = 's' 115 [61] = 't' 116 [62] = 's' 115

Here is my code: private class GetBlogPostsTask extends AsyncTask <Object, Void, String> {

    @Override
    protected String doInBackground(Object[] objects) {

        int responseCode = -1;
        try {

            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
            HttpURLConnection connection = (HttpURLConnection)blogFeedUrl.openConnection();
            connection.connect();
            responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK){
                InputStream inputStream = connection.getInputStream(); //store the data into the input stream
                Reader reader = new InputStreamReader(inputStream); //read the input stream
                int contentLength = connection.getContentLength(); //get the number of characters to read in
                char[] charArray = new char[contentLength];  //create the char array to store the the data
                reader.read(charArray); //read and store the data array into the char array
                String responseData = new String(charArray); //create a new string and convert and store from char to string

                //get the string data
                JSONObject jsonResponse = new JSONObject(responseData);
                String status = jsonResponse.getString("status");


                JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
                for(int i = 0; i < jsonPosts.length(); i++){
                    JSONObject jsonPost = jsonPosts.getJSONObject(i);
                    String title = jsonPost.getString("title");
                    Log.v(TAG, "Post " + i + ": " + title);

                }

Can you please advise?

protected JSONObject doInBackground(Object[] objects) { .... JSONObject jsonResponse = new JSONObject(responseData); return jsonResponse; }

I think you need to return the JSONObject object after you read that from the blog URL.