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 Concurrency and Error Handling Making Our Code Asynchronous

Greg Mosley
Greg Mosley
2,495 Points

No JSON Data landing in the log

Hi,

No JSON data is showing in the log and I cannot work out the reason why. I've checked my code matches what is in the tutorial, and cannot see anything obvious in the logcat.

CODE:

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

    String apiKey = "7ce1dbe5c41a6d917f918337a66373c6";
    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(@NotNull Call call, @NotNull IOException e) {
        }

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

}

LOGCAT (separated each line for ease):

2019-07-21 10:42:53.273 25258-25258/? I/.example.storm: Not late-enabling -Xcheck:jni (already on)

2019-07-21 10:42:53.315 25258-25258/? E/.example.storm: Unknown bits set in runtime_flags: 0x8000

2019-07-21 10:42:53.316 25258-25258/? W/.example.storm: Unexpected CPU variant for X86 using defaults: x86

2019-07-21 10:42:54.283 25258-25258/com.example.stormy W/.example.storm: JIT profile information will not be recorded: profile file does not exits.

2019-07-21 10:42:54.287 25258-25258/com.example.stormy I/chatty: uid=10137(com.example.stormy) identical 10 lines

2019-07-21 10:42:54.287 25258-25258/com.example.stormy W/.example.storm: JIT profile information will not be recorded: profile file does not exits.

2019-07-21 10:42:54.329 25258-25258/com.example.stormy I/InstantRun: starting instant run server: is main process

2019-07-21 10:42:54.860 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)

2019-07-21 10:42:54.861 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)

2019-07-21 10:42:55.545 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setUseSessionTickets(Z)V (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.546 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setHostname(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.546 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.546 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.547 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.548 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.548 25258-25258/com.example.stormy W/.example.storm: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)

2019-07-21 10:42:55.553 25258-25258/com.example.stormy D/NetworkSecurityConfig: No Network Security Config specified, using platform default

2019-07-21 10:42:55.879 25258-26007/com.example.stormy D/OpenGLRenderer: Swap behavior 1

2019-07-21 10:42:55.880 25258-26007/com.example.stormy W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

2019-07-21 10:42:55.880 25258-26007/com.example.stormy D/OpenGLRenderer: Swap behavior 0

2019-07-21 10:42:55.896 25258-26007/com.example.stormy D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0

2019-07-21 10:42:55.896 25258-26007/com.example.stormy D/EGL_emulation: eglCreateContext: 0xdee1a1e0: maj 3 min 1 rcv 4

2019-07-21 10:42:55.900 25258-26007/com.example.stormy D/EGL_emulation: eglMakeCurrent: 0xdee1a1e0: ver 3 1 (tinfo 0xdee0f490)

2019-07-21 10:42:55.902 25258-26007/com.example.stormy E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da

2019-07-21 10:42:55.905 25258-26007/com.example.stormy E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da

2019-07-21 10:42:55.950 25258-26007/com.example.stormy W/Gralloc3: mapper 3.x is not supported

2019-07-21 10:42:55.969 25258-26007/com.example.stormy D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0

2019-07-21 10:42:56.022 25258-26007/com.example.stormy D/EGL_emulation: eglMakeCurrent: 0xdee1a1e0: ver 3 1 (tinfo 0xdee0f490)

2019-07-21 10:42:56.026 25258-26007/com.example.stormy D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0

2019-07-21 10:42:56.597 25258-26007/com.example.stormy I/OpenGLRenderer: Davey! duration=845ms; Flags=1, IntendedVsync=44220965672004, Vsync=44220999005336, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=44221005661700, AnimationStart=44221005743800, PerformTraversalsStart=44221005812100, DrawStart=44221235278000, SyncQueued=44221237427800, SyncStart=44221287367000, IssueDrawCommandsStart=44221287471500, SwapBuffers=44221733501900, FrameCompleted=44221860663400, DequeueBufferDuration=5497000, QueueBufferDuration=1881000,

4 Answers

Greg Mosley
Greg Mosley
2,495 Points

Strange, after doing some digging around online (https://stackoverflow.com/questions/56266801/java-net-socketexception-socket-failed-eperm-operation-not-permitted) I added the user permission for "ACCESS_NETWORK_STATE" and also uninstalled the app from my emulator and reinstalled and now it works.

Unclear which of the two solutions worked, but going to leave it as is!

Wynston Champion
Wynston Champion
16,728 Points

I've been digging around for hours and hours on this and uninstalling the app from the emulator did the trick! Just to note, I had already added the user permission for "ACCESS_NETWORK_STATE", but didn't uninstall the app. Weird how this is what solved this.

Blake Harmon
Blake Harmon
4,876 Points

Was having the same issue, simply uninstalling and reinstalling the app fixed it for me.

Adolfo Obregon
Adolfo Obregon
10,156 Points

Same here, uninstalling fixed the problem.

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