java.net.UnknownHostException: 无法解析主机“<url here>”;没有与主机名关联的地址
java.net.UnknownHostException: Unable to resolve host “<url here>”; No address associated with hostname
我正在使用 AsychTask 函数调用本地主机服务器的 URL“http://xx2/postdata/DATA2.asp?count=1&CIHAZID=53&ISLEMZAMANI=18102016190815&GZBHID=28198”。在具有正确且有效的以太网连接的 Android 设备上调试时,它没有得到 "OK" 响应而是生成 UnKnownHostException
我已经尝试了不同站点和页面上提到的所有解决方案,例如 "add internet and network access permissions in manifest file" 或 "change the targetSdkVersion to 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.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.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
您提到 url 是从您的本地主机(可能是在您的家庭网络基础设施中充当服务器的台式电脑)提供服务的,您的本地主机是否名为 xx2
?
您应该通过 IP 访问它,例如http://192.168.1.1/....
因为您的 android 设备不知道名称 xx2
解析为您服务器的 IP。
我正在使用 AsychTask 函数调用本地主机服务器的 URL“http://xx2/postdata/DATA2.asp?count=1&CIHAZID=53&ISLEMZAMANI=18102016190815&GZBHID=28198”。在具有正确且有效的以太网连接的 Android 设备上调试时,它没有得到 "OK" 响应而是生成 UnKnownHostException
我已经尝试了不同站点和页面上提到的所有解决方案,例如 "add internet and network access permissions in manifest file" 或 "change the targetSdkVersion to 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.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.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
您提到 url 是从您的本地主机(可能是在您的家庭网络基础设施中充当服务器的台式电脑)提供服务的,您的本地主机是否名为 xx2
?
您应该通过 IP 访问它,例如http://192.168.1.1/....
因为您的 android 设备不知道名称 xx2
解析为您服务器的 IP。