UnknownHostException: name or service not known

Jordan Macey-Smith picture Jordan Macey-Smith · Oct 9, 2015 · Viewed 50.1k times · Source

I'm attempting to return some data from an API using OkHttpClient in com.squareup.okhttp. I've run into a few errors that i have eventually been able to overcome but i can't get past this host exception error and nothing on here seems to be specific enough to my case to be able to solve.

Below is the code i have attempted along with the output with it, if anyone has any idea how to overcome the error i would appreciate it.

CODE

public void connect() {         
    OkHttpClient client = new OkHttpClient();

    com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
    .url("https://socialweb-analytics.lcloud.com/api/public/reports/jobs?companyKey=ato")
    .get()
    .addHeader("authorization", "Basic c2RidXNpbmVzc2FuYWx5dGljc0BhdG8uZ292LmF1OkFuYWx5dGljezEh")
    .addHeader("cache-control", "no-cache")
    .addHeader("postman-token", "65ef5553-77b5-053f-9c01-4fdf76bdc92f")
    .build();

    System.out.println(request.toString());

    try {
        Response response = client.newCall(request).execute();
        System.out.println(response);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

OUTPUT

Request{method=GET, url=https://socialweb-analytics.lcloud.com/api/public/reports/jobs?companyKey=ato, tag=null}
java.net.UnknownHostException: socialweb-analytics.lcloud.com: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:922)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1314)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1267)
    at java.net.InetAddress.getAllByName(InetAddress.java:1183)
    at java.net.InetAddress.getAllByName(InetAddress.java:1119)
    at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
    at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
    at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
    at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
    at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
    at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
    at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
    at com.squareup.okhttp.Call.getResponse(Call.java:267)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
    at com.squareup.okhttp.Call.execute(Call.java:79)
    at technical_services.persistence.lithium.LithiumDataStore.connect1(LithiumDataStore.java:127)
    at application.lithium_etl.LithiumTestController.main(LithiumTestController.java:24)

Answer

zhenbo xu picture zhenbo xu · Apr 23, 2017

For error like "java.net.UnknownHostException: [hostname]"

The reason is your hostname is not in /etc/hosts, The solution is simple:

sudo vim /etc/hosts

change the line looks like:

127.0.0.1  localhost

to:

127.0.0.1  [hostname] localhost

Save and exit. If the problem still exist, may be you need to restart or run :

sudo ifconfig eth0 down&&sudo ifconfig eth0 up

Hope it can help you!