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 Getting JSON Data from an HTTP Request

Lean Karlo Corpuz
Lean Karlo Corpuz
6,159 Points

Im having an error responseCode returns -1

here is my code

@Override
    protected String doInBackground(Object... arg0){

        int responseCode = -1;

        //PostActivity getBlogPostsTask = new PostActivity();
        try
        {
            URL blogFeedUrl = new URL("http://localhost/medapp/public/mobile/post");
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();

            if (responseCode == 200)
            {
                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);

            }
            else
            {
                Log.i("TAG", "Unsuccessful HTTP Response Code: " + responseCode);
            }



        }
        catch (MalformedURLException e)
        {
            Log.e("TAG", "exception caught: ", e);
        }
        catch (IOException e)
        {
            Log.e("TAG", "exception caught: ", e);
        }
        catch (Exception e)
        {
            Log.e("TAG", "exception caught: ", e);
        }

log 05-04 12:14:31.215 2181-2194/medapp.app E/TAG? exception caught: java.lang.NegativeArraySizeException: -1 at medapp.app.PostActivity.doInBackground(PostActivity.java:38) at medapp.app.PostActivity.doInBackground(PostActivity.java:17) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841)

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I'm guessing that the contentLength variable is getting set to a negative value. There is a known bug in the code that can be fixed by using alternative code that is available in the Teacher's Notes of this video. Sorry for the confusion!