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 trialSara Nocentini
1,157 PointsHelp on Challenge Getting JSON Data from an HTTP Request (Task 5)?
Everything is fine untill I get to the last task where I am supposed to, set the 'responseData' String variable with the data in 'charArray'. Convert it using the String constructor that takes a char array as its parameter.
I wrote the following code:
String responseData = new String(charArray);
but If I enter it tells me that
JavaTester.java:73: responseData is already defined in run( String responseData = new String(charArray);
Indeed it is defined in the line 1, but If I delete that line I got 2 errors:
JavaTester.java:134: cannot find symbol symbol : variable responseData location: class JavaTester if (responseData.equals("")) { ^ JavaTester.java:137: cannot find symbol symbol : variable responseData location: class JavaTester else if (!responseData.equals("STAGE3TEST")) { ^
I don't know how to solve it. Can anyone Help me?
3 Answers
Matt L
Courses Plus Student 1,846 PointsHey Sara,
I was also stuck on the same problem. As best as I can describe it, I believe that when you use "String" before the variable, you are declaring it as a new String. Since the variable "responseData" has already been declared as a String, doing it again results in the error. Try removing "String" from your code, and simply refer to "responseData" directly.
Hope this helped! Matt
eric hughes
12,345 PointsString responseData = "";
try { URL treehouseUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=20"); HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection(); connection.connect(); int 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);
responseData = new String(charArray);
// Code to read InputStream goes here!
}
} catch (MalformedURLException e) { Log.e(TAG, "MalformedURLException caught!", e); } catch (IOException e) { Log.e(TAG, "IOException caught!", e); }
Calvin Nix
43,828 PointsHey Sara,
Do you mind providing the full code you wrote?
Thanks, Calvin
Sara Nocentini
1,157 PointsSara Nocentini
1,157 PointsThanks, it workd! As you said I had to remove String from the code. Thank you so much, Sara