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


Making our code Asynchronous: Android Studio won't allow IOException in catch block, no weather data in logcat

Android Studio(Latest Build) does not allow the catch block and says IOException is never thrown. App compiles and runs but logcat only returns:

03-04 11:09:18.221 14450-14476/com.thetechguys.mbepo V/MainActivity﹕ com.squareup.okhttp.internal.http.RealResponseBody@41ed0b98

Here is my code:

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.thetechguys.mbepo.R;

import java.io.IOException;

public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    protected void onCreate(Bundle savedInstanceState) {

        String apiKey = "3b69dd09645b01d78c4996ed7c425744";
        double latitude = 37.8267 ;
        double longitude = -122.423;
        String forecastURL = "https://api.forecast.io/forecast/" + apiKey + "/" + latitude + "," + longitude;

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(forecastURL).build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            public void onFailure(Request request, IOException e) {


            public void onResponse(Response response) throws IOException {

                if (response.isSuccessful()) {
                    Log.v(TAG, response.body().toString());




1 Answer

My issue was the wrong chain constructor on the returned 'response', I used toString() instead of string(). Now it works. Guess we'd also need to explicitly catch the IOException in onFailure if we want to declare another local IOException in try/catch block in onResponse.