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

Android Build a Weather App (2015) Concurrency and Error Handling What To Do When the Network is Down

Martin Olsson
Martin Olsson
449 Points

FATAL EXCEPTION: OkHttp Dispatcher

Hi,

I get an error message which I can't find a solution for on the internet, and the previous solution of deleting the Log statement doesn't work.

Code: package martinsoft.stormy;

import android.content.Context; import android.inputmethodservice.KeyboardView; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; import android.support.v4.net.ConnectivityManagerCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call; import okhttp3.Callback; import okhttp3.Headers; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String apiKey = "7bc9ae2a7c90f4eeb54caedd881cbc1a";
    double latitude = 37.8267;
    double longitude = -122.423;
    String forecastUrl = "https://api.darksky.net/forecast/" + apiKey +
            "/" + latitude + "," + longitude;

    if (isNetworkAvailable() ) {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();
        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 {
                    response = call.execute();
                    if (response.isSuccessful()) {

                    }
                } catch (IOException e) {
                    //Log.e(TAG, "Exception caught: ", e);
                }

            }

        });
    }
}

private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    boolean isAvailable = false;

    if (networkInfo != null && networkInfo.isConnected() ) {
        isAvailable = true;

    } else {
        Toast.makeText(this, R.string.network_unavailable_message,Toast.LENGTH_LONG);
    }
    return isAvailable;
}

}