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 Making Our Code Asynchronous

Rami Awar
Rami Awar
5,435 Points

Still getting a network on main thread error

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

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

    String apiKey = "2563b99e16765852128947bde555f758";
    double lat = 37.8267;
    double lon = -122.423;
    String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + "/" + lat + "," + lon;

    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(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            try {
                if(response.isSuccessful()){
                    Log.v(TAG, response.body().string());
                }
            } catch (IOException e) {
                Log.e(TAG, "piss piss piss: ", e);
            }

        }
    });



}

This is still giving me a network on main thread error. Why hasn't this been fixed? Did I miss something?