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

时间:2015-07-09 13:14:10

标签: android http android-wifi android-4.4-kitkat okhttp

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

  

无法解析主持人&#34; www.xxx-xxxxxxx.com&#34;:没有与主机名相关联的地址

但我不会遇到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");
}
}

在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文件:

<?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>

日志:

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

0 个答案:

没有答案