【问题标题】:java.net.UnknownHostException: Unable to resolve host “<url here>”; No address associated with hostnamejava.net.UnknownHostException:无法解析主机“<url here>”;没有与主机名关联的地址
【发布时间】:2016-11-18 13:03:44
【问题描述】:

我正在使用 AsychTask 函数来调用 localhost 服务器的 URL“http://xx2/postdata/DATA2.asp?count=1&CIHAZID=53&ISLEMZAMANI=18102016190815&GZBHID=28198”。在具有正确且有效的以太网连接的 Android 设备上进行调试时,它不会得到“OK”响应,而是产生 UnKnownHostException

我已经尝试了不同站点和页面上提到的所有解决方案,例如“在清单文件中添加互联网和网络访问权限”,或“将 targetSdkVersion 更改为 22”等等......

清单文件中已经设置了互联网权限。

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

如果我在 android 设备的浏览器中调用 URL,它会得到“OK”作为响应,但我的应用程序没有。以下是我用来调用 URL 的 AsyncTask 函数

// AsyncTask Function to send TempReports Table records to server
private class sendLogsToServer extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        URLConnection feedUrl;
        StringBuilder sb = new StringBuilder();
        try {
            feedUrl = new URL(params[0]).openConnection();
            InputStream is = feedUrl.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "");
            }
            is.close();

            //createUserPageResult = sb.toString();
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // if Response from server is OK, then delete the entry from DB TempReports Table
        if (result != null) {
            if (result.equals("OK") || result.equals("updated")) {
                //Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
                posDatabase.tempLogsDelete(logToDelete);
            }
        } else {
            // don't delete
            //Toast.makeText(MainActivity.this, "NOT Deleted", Toast.LENGTH_SHORT).show();
        }
    }
}

在日志中显示无法解析主机 xx2。 以下是更清晰的日志。

11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err: java.net.UnknownHostException: Unable to resolve host "xx2": No address associated with hostname
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at com.emiturk.pos.MainActivity$sendLogsToServer.doInBackground(MainActivity.java:507)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at com.emiturk.pos.MainActivity$sendLogsToServer.doInBackground(MainActivity.java:493)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.lang.Thread.run(Thread.java:856)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
11-18 15:30:32.300 13867-14241/com.emiturk.pos W/System.err:    ... 21 more

【问题讨论】:

  • 你的Base url问题,能否请从浏览器天气中检查你的base url是否有效
  • 你的代码中的url在哪里
  • 您的设备正在使用的 DHCP 服务器是否知道主机名“xx2”?尝试通过 abd 使用 adb shell 连接到您的设备来确保这一点,然后输入 ping xx2。我想,结果会告诉你,xx2 是未知的。
  • 我将 url 字符串传递给 asyncTask 函数

标签: android exception localhost unknown-host


【解决方案1】:

您提到该 url 是从您的 localhost(可能是作为家庭网络基础设施中的服务器的台式电脑)提供的,您的 localhost 是否命名为 xx2

您应该通过 IP 访问它,例如http://192.168.1.1/.... 因为您的安卓设备不知道名称 xx2 解析为您的服务器的 IP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多