java.net.UnknownHostException:无法解析主机“<url here =”“>”;没有与主机名关联的地址

时间:2016-11-18 13:03:45

标签: android exception localhost unknown-host

我正在使用AsychTask函数调用URL&#34; http://xx2/postdata/DATA2.asp?count=1&CIHAZID=53&ISLEMZAMANI=18102016190815&GZBHID=28198&#34; localhost服务器。在Android设备上进行调试时,使用适当且有效的以太网连接,而不是获得&#34; OK&#34;响应其生成的UnKnownHostException

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

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

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

如果我在Android设备的浏览器中调用它,它就会得到&#34; OK&#34;作为回应,但我的申请却没有。以下是我用来调用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

1 个答案:

答案 0 :(得分:0)

您提到该网址是从您的localhost(可能是作为家庭网络基础架构中的服务器的桌面PC)提供的,您的本地主机名为xx2吗?

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