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 trialGracious Chinyama
3,292 Pointscall.enqueue code block is getting skipped
Hey guys, I've copied the code in the video verbatim but for some reson the JSON from dark sky isin't appearing in my log as expected. I put a breakpoint on line 47 (the try block) and as it turns out that code block is never executed. I also checked if forecastURL was holding a valid link by copying and pasting it's value into chrome and chrome returned the expected JSON.
I've been over my code line by line to check if I'm missing a bracket or some other syntax but I couldn't find anything. Thanks for the help, here is my code:
call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
// Response res = call.execute();'this is a synchronous method
Log.v(Tag, jsonData);
Log.v(Tag, "Code ran successfully");
if (response.isSuccessful()) {
} else {
alertUserAboutError();
}
} catch (IOException e) {
e.printStackTrace();
Log.e(Tag, "IO Exception caught", e);
}
catch (JSONException e){
Log.e(Tag ,"Catch JSONException",e);
}
Log.d(Tag, "Our code is running");
}
});
1 Answer
Paritosh Sahni
Courses Plus Student 4,853 PointsHey Gracious,
I couldn't find an error in your code. Also the onResponse method will be called back on its own so the try should execute normally. But anyways you can try a few things.
Since you are doing Log.v(), make sure that in Logcat you chose the VERBOSE tag from the drop down menu.
Also you can remove "String jsonData = response.body().string();" from the try-catch block and then add the Log.d() or Log.v() just below it. So both these statements out of the try block. Then debug again. You can remove this line as your already throwing an exception to the method that will be calling this onResponse() method.
Try these steps and check the Logcat. This is the best I know and if this doesn't work then someone else will definitely help you.
Cheers!