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