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
fabh
946 PointsNegativeArraySizeException Blog Reader App
the app works perfect until i change the url from http://blog.teamtreehouse.com/api/get_recent_summary/?count=20
to something else like http://graph.facebook.com/coca-cola
then it throws me a NegativeArraySizeException..
what am I doing wrong?
I think the error is somewhere here, but I don't know where exactly ..
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);
it kinda works for this url https://graph.facebook.com/shaverm/picture but not for others..
4 Answers
fabh
946 Pointsno one? :/
Ben Jakuben
Treehouse TeacherHi @Fabian,
So the original app is requesting JSON data of a specific format from the Treehouse blog. The code you pasted should get and log data from a web request:
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);
Where you'll likely run into trouble is trying to do anything else with that data. The rest of the app is based on the data format being returned by the Treehouse blog, so you'd need to adjust how you parse the data if you request from a different URL like the ones you posted.
Now, as to why you're getting a NegativeArraySizeException...can you see in Eclipse which line of code is throwing the exception? What you posted seems okay, so it might be somewhere else in your code where it's failing. Depending on how far you are in the Blog Reader course, code added later (parsing the data returned from the web) would definitely cause it to break if the format is different. But you should be able to simply write the data to the log.
Renaldo Pierre-Louis
717 PointsI am having the same problem. It works fine with the treehouse url, but no others. For some reason getContentLength() is return -1. (I have commented code after reader.read(), so the problem isn't with the format of the JSON data.
Renaldo Pierre-Louis
717 Pointsah, the server isn't setting Content-Length in the header... I think that should fix it.