Search code examples
javahttpdnsdialogflow-esunknown-host

api.api.ai: Temporary failure in name resolution


With my code (shown below), I get proper result randomly. But most of the time I get error.

EXCEPTION:

java.net.UnknownHostException: api.api.ai: Temporary failure in name resolution
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
..<snip>...
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at PostJSON.main(PostJSON.java:34)

SSCCE CODE:

private static final String URL = "https://api.api.ai/v1/query?v=20150910";
private static final String ACCESS_CODE = "XXXXXX";
public static final HttpClient client = HttpClientBuilder.create().build();
public static final HttpPost httppost = new HttpPost(URL);

private static final String str_json = "{ query: \"how are you\", lang: \"en\", sessionId: \"somerandomthing\" }";

public static void main(String[] args) {

    try {

        HttpEntity entity = new ByteArrayEntity(str_json.getBytes("UTF-8"));
        httppost.setHeader("Content-Type",
                "application/json; charset=utf-8");
        httppost.setHeader("Authorization", "Bearer " + ACCESS_CODE);
        httppost.setEntity(entity);
        HttpResponse response = client.execute(httppost);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
    } catch (Exception ex) {
        ex.printStackTrace();
    } 
}

NSLOOKUP:

zeek@zeek:$ nslookup api.ai.ai
Server:     127.0.1.1  
Address:    127.0.1.1#53

Non-authoritative answer:  
Name:   api.ai.ai  
Address: 92.242.140.21

/ETC/HOSTS

127.0.0.1   localhost
127.0.1.1   zeek

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Note: It does work sometime.

Please suggest what setting/config should I be checking from my side.

also note, my ping to api.api.ai works fine.


Solution

  • Posting solution to help others facing same problem,

    On the lines of what @Hackerman said:

    My wifi-extender was not capable to handle IPV6 translation. Always check the wifi-extender spec when encountered with similar problem.

    As a solution/hack (if you HAVE to use IPV4-only router) use following when starting java

    -DpreferIPv4Stack