OkHttp - 无法解析主机<url>:没有关联的地址

She*_*rib 8 android http android-wifi okhttp android-4.4-kitkat

我向我的服务器发出一个简单的HTTP GET请求,但过了一段时间我在使用wifi时遇到了这个问题:

无法解析主机"www.xxx-xxxxxxx.com":没有与主机名关联的地址

但是我对3G没有这个问题.

我正在Xperia平板电脑上运行应用程序Z和Wifi完美无瑕.可能是ISP问题还是我的代码?

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");
}
}
Run Code Online (Sandbox Code Playgroud)

在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();
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

日志:

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
Run Code Online (Sandbox Code Playgroud)