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 trialJos s
Courses Plus Student 44 PointsProblem with compile error - responseData
Task:
Finally, set the 'responseData' String variable with the data in 'charArray'. Convert it using the String constructor that takes a char array as its parameter.
String 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) {
// Code to read InputStream goes here!
InputStream is = connection.getInputStream();
Reader reader = new InputStreamReader(is);
char[] charArray = new char[connection.getContentLength()];
reader.read(charArray);
String responseData = new String(charArray);
}
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException caught!", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caught!", e);
}
What's problem with this. Compiler says: JavaTester.java:74: responseData is already defined in run()
String responseData = new String(charArray);
^
1 error
2 Answers
Chris Shaw
26,676 PointsHi Jos,
This error is occurring because you're attempting to refine the variable responseData
which you've already defined before the try/catch block, simply remove the String
typecast from the try catch and it should work as expected.
responseData = new String(charArray);
Jos s
Courses Plus Student 44 PointsSo the solution is to remove it out and place before or after in try/catch block ?
Chris Shaw
26,676 PointsNo, the try/catch block isn't the issue, the problem is you've already defined responseData
outside of the try/catch block and have also attempted to define it again within the try/catch block which won't work as variables with the same name can only be defined once within the same scope.
Currently you have the below.
reader.read(charArray);
String responseData = new String(charArray);
Instead what you want is the following, I've shortened the code quite a bit but have retained the overall structure.
// Define the `responseData` variable
String responseData = "";
try {
// Connection code here...
if (responseCode == HttpURLConnection.HTTP_OK) {
// Assign the response text to the already defined `responseData` variable
responseData = new String(charArray);
}
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException caught!", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caught!", e);
}
Hope that makes it's a little more clear.