Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Weather App Concurrency and Error Handling Making Our Code Asynchronous

Nicolas Connor
Nicolas Connor
3,410 Points

call.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:

package com.example.nico.stormy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

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 = "bda6379909ad245e0d0882cbe7a68c7a";

        double latitude = 37.8267;
        double longitude = -122.4233;

        String forecastURL = "https://api.darksky.net/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() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    if (response.isSuccessful()){
                        Log.v(TAG, response.body().string());
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception caught: ",e);
                }
            }
        });




    }
}

I fixed it just like he said in the video, make sure your url is working properly. I had a missing "/" at the end of the first string within my forecastURL.

1 Answer

I'm not sure what version of Android you were using but there has been a behavior change to all apps that target Android 9 Pie and above. By default Http requests don't work. Because the request is not made to Log v does not appear in the Logcat. In order to make http network requests you have to specify it in the Android Manifest file. In the application tag add android: usesCleartextTraffic="true". See stack overflow:

https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie