Sudhanshu Gaur
Sudhanshu Gaur

Reputation: 7684

Android Retrofit/OkHttp Use `8.8.8.8` programatically for DNS lookup?

I am trying to add Google DNS lookup which is '8.8.8.8.' in my android app, I am using Retrofit with OkHttp.

Is it possible programmatically?

I saw this post but wasn't able to add it dns-android-okhttp.

Upvotes: 2

Views: 6397

Answers (3)

Sudhanshu Gaur
Sudhanshu Gaur

Reputation: 7684

It is working now actually in the dns-android-okhttp I was searching, I was looking for which library he is using for Resolver which was dnsjava

compile 'dnsjava:dnsjava:2.1.7'

And now after adding it, my code is working correctly.

Upvotes: 2

Yuri Schimke
Yuri Schimke

Reputation: 13488

It's possible but awkward to opt into Google 8.8.8.8 regular DNS, you need to write your own DNS implementation using a library like Netty or dnsjava. Here's one I just whipped up.

e.g. https://github.com/yschimke/okurl/blob/release/1.57/src/main/kotlin/com/baulsupp/okurl/network/NettyDns.kt

Upvotes: 1

Yuri Schimke
Yuri Schimke

Reputation: 13488

If you want to try DNS over HTTPS querying Google (not technically 8.8.8.8) then it's supported as an experimental module within OkHttp

OkHttpClient bootstrapClient = new OkHttpClient();

Dns google = new DnsOverHttps.Builder().client(bootstrapClient)
    .url(HttpUrl.get("https://dns.google.com/experimental"))
    .build();
OkHttpClient client = new OkHttpClient.Builder().dns(google).build();

Response result =
    client.newCall(new Request.Builder().url("https://google.com/robots.txt").build())
        .execute();

System.out.println(result.body().string());

Upvotes: 5

Related Questions