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

codeoverload
codeoverload
24,260 Points

UnknownHostException: Unable to resolve host "api.darksky.net": No address associated with hostname

I followed the video and I have the exact same code (except some parts because of okhttp3). But when I run the app there is an "UnknownHostException".

Here is my log:

11-17 19:39:01.847 9867-9895/com.codeoverload.stormy W/System: ClassLoader referenced unknown path: /data/data/com.codeoverload.stormy/lib
11-17 19:39:01.858 9867-9867/com.codeoverload.stormy D/MainActivity: https://api.darksky.net/forecast/1fb52342ec84c2f2631b797ff48a6a03/37.8267,-122.4233
11-17 19:39:01.859 9867-9867/com.codeoverload.stormy D/MainActivity: Main UI code is running!
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err: java.net.UnknownHostException: Unable to resolve host "api.darksky.net": No address associated with hostname
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:470)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.Dns$1.lookup(Dns.java:39)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:172)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:138)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:80)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:178)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.RealCall.access$100(RealCall.java:33)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at java.lang.Thread.run(Thread.java:818)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
11-17 19:39:01.860 9867-24113/com.codeoverload.stormy W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
11-17 19:39:01.861 9867-24113/com.codeoverload.stormy W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
11-17 19:39:01.861 9867-24113/com.codeoverload.stormy W/System.err:     ... 27 more
11-17 19:39:01.861 9867-9914/com.codeoverload.stormy E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa0977870
11-17 19:39:03.854 9867-9914/com.codeoverload.stormy E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa18724e0  

and here is my code:

package com.codeoverload.stormy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 = "**hidden_API_key**";
        double latitude = 37.8267;
        double longitude = -122.4233;
        String forecastUrl = "https://api.darksky.net/forecast/" + apiKey +
                "/" + latitude + "," + longitude;

        Log.d(TAG, forecastUrl);

        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) {
                e.printStackTrace();
            }

            @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, "Exception caught: ", e);
                }
            }
        });

        Log.d(TAG, "Main UI code is running!");


    }
}   

I tried using code from the okhttp3 docs and from stackoverflow but it didn't work.

Thank you in advance!

Seth Kroger
Seth Kroger
56,413 Points

If you open the web browser on your device/emulator are you able to navigate to sites like google.com? (i.e., Do you have working Internet?)

codeoverload
codeoverload
24,260 Points

Oh, that was the problem. Tried on my nexus and it worked.
Thank you very much!
still have one question: How can I connect my emulator to the internet?

codeoverload
codeoverload
24,260 Points

Never mind, I figured it out. :)