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 trialMohammed Safiulla D
17,044 PointsNot getting the CalledFromWrongThreadException when accessing data from the background thread
During Build a Weather app course (->Hooking Up the Model to the View->Weather Icon video), When setDrawable()
method was called from a background thread in the video there was a calledFromWrongThredException
which was later handled by runOnUI()
; but I am not getting the exception and when I run the app it is updating the UI from the background thread.
Has something changed in Android thread implementation (so that now this is also possible now) or am I doing something wrong? Here is my code:
String forecastURL = "https://api.darksky.net/forecast/" + API_KEY + "/" + latitude + "," + longitude;
if(isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastURL)
.build();
Call call = client.newCall(request);
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();
Log.v(TAG, "here is the code" + jsonData);
if (response.isSuccessful()) {
currentWeather = getCurrentDetails(jsonData);
CurrentWeather displayWeather = new CurrentWeather(
currentWeather.getLocationLabel(),
currentWeather.getIcon(),
currentWeather.getTime(),
currentWeather.getTemperature(),
currentWeather.getHumidity(),
currentWeather.getPrecipChance(),
currentWeather.getSummary(),
currentWeather.getTimeZone()
);
binding.setWeather(displayWeather);
Drawable drawable = getResources().getDrawable(displayWeather.getIconId());
//Suppossed to ge an error here...
iconImageView.setImageDrawable(drawable);
} else {
alertUserAboutError(getString(R.string.error_dialog_message_unsuccessful_response));
}
} catch (IOException e) {
Log.e(TAG, "IO exception caught: ", e);
} catch (JSONException e) {
Log.e(TAG, "JSON exception caught: ", e);
}
}
});
} else {
alertUserAboutError(getString(R.string.error_dialog_message_network_unavailable));
}
P.S Feels weird to ask why I am not getting an error rather than the usual 'how to fricking solve one'!