10

I make a simple HTTP GET request to my server but after a while I encounter with this problem when using wifi:

Unable to resolve host "www.xxx-xxxxxxx.com": No address associated with hostname

But I don't have this problem with 3G.

I'm running app on Xperia tablet Z and Wifi works flawlessly. Could it be ISP problem or my code?

public class OkHttpIsAlive {
private final OkHttpClient client = new OkHttpClient();
private final String URL = "http://www.xxx-xxxx.com/upload/alive.php";

public void run() throws Exception {

    client.setConnectTimeout(20, TimeUnit.SECONDS);
    client.setWriteTimeout(20, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);

    Request request = new Request.Builder()
            .url(URL)
            .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Log.i("IsAlive", "isAlive is working");
}
}

In MainActivity:

new Thread(new Runnable() {
        @Override
        public void run() {
            OkHttpIsAlive isAlive = new OkHttpIsAlive();
            while (threadRunning) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    Log.e(TAG, e.getMessage());
                }

                try {
                    isAlive.run();
                }catch (Exception e) {
                    Log.e("IsAlive", "err->" + e.getLocalizedMessage());
                }
            }
        }
    }).start();

AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.mytest" >    

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>

</application>

Log:

I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
D/IsAlive ( 5297): err->failed to connect to www.xxx-xxxxxx.com/185.8.173.26 (port 80) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
I/IsAlive ( 5297): isAlive is working
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
D/IsAlive ( 5297): err->Unable to resolve host "www.xxx-xxxxxx.com": No address associated with hostname
Shervin Gharib
  • 728
  • 2
  • 14
  • 29
  • weird. i am have similar problem. everything worked useing my wifi via LTE connection. then suddenly my wifi could not connect to DNS. rebooted my LTE modem and internet back up but now get OkHttp failed error, "unable to resolve host....No address associated with hostname" if i connect via my mobile data provider, everything works. so definitely something to do with my LTE internet connection and not OkHttp. – Clive Sargeant Mar 29 '17 at 14:09
  • further to my above comment. i used postman on my laptop over the same LTE internet connection to call the same API and it worked. so problem is possibly with Android? – Clive Sargeant Mar 29 '17 at 15:54
  • 1
    Ah... I have to reopen my Emulator. Emulator was showing Wifi `Connected, no Internet Access` error for some reason. Reopening it worked. – Suryavel TR Nov 08 '18 at 15:56

0 Answers0