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

Ayman Bitar
Ayman Bitar
9,462 Points

contentlength of the Inputstream is -1

Hello,

I've watched the course on starting the Blogreader App, and it worked perfectly for me.

Now i'm trying to do with a different website, however the contentLength is always returning -1. I'm not sure if its a problem from the site that i'm getting from or something else.

This is the link of the website: www.kippreport.com/?json=1

Thank you in advance.

Best, Ayman

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry - this is a known error! We discussed this in the Forum a while back and have linked to it as a "Known Issue" in the Teacher Notes on the video page. Check out the solution (and discussion) in that other thread. I will loop back and update the project at some point, too.

Other posts about this issue:

Ayman Bitar
Ayman Bitar
9,462 Points

Thanks for this. It really helped. I have replied below with the exact lines to change, so anyone else could just do it easily.

Ayman Bitar
Ayman Bitar
9,462 Points

Hi Ben,

Thank you very much for the solution. It's working perfectly. I used the below solution.

If you followed the tutorials exactly inside the GetBlogPostTask class, replace the following lines:

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();
                Reader reader = new InputStreamReader(inputStream);
                int contentLength = connection.getContentLength();
                System.out.println(contentLength);
                char[] charArray = new char[contentLength];
                reader.read(charArray);
                String responseData = new String(charArray);

                jsonResponse = new JSONObject(responseData);
            }

with the following:

StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);

        try {
            HttpResponse response = client.execute(httpget);
            StatusLine statusLine = response.getStatusLine();
            responseCode = statusLine.getStatusCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while((line = reader.readLine()) != null){
                    builder.append(line);
                }
                String s = builder.toString();
                jsonResponse = new JSONObject(s);
            }

Its the solution that Fabian Hippmann provided in this link https://teamtreehouse.com/forum/blogreader-app-json-exception#show-comment-11122

Thanks again.

Best, Ayman