java Java未知主机异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4427487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java unknown host exception
提问by Ayaz Alavi
I am trying to access textalertapp.com via HTTP Post request from my android application. But I am getting Unknown host error. Can anybody help me solve this issue.
我正在尝试通过来自我的 android 应用程序的 HTTP Post 请求访问 textalertapp.com。但我收到未知主机错误。谁能帮我解决这个问题。
12-13 01:30:16.058: WARN/System.err(473): java.net.UnknownHostException: textalertapp.com
12-13 01:30:16.088: WARN/System.err(473): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
12-13 01:30:16.088: WARN/System.err(473): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
12-13 01:30:16.088: WARN/System.err(473): at java.net.InetAddress.getAllByName(InetAddress.java:242)
12-13 01:30:16.088: WARN/System.err(473): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
12-13 01:30:16.099: WARN/System.err(473): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-13 01:30:16.099: WARN/System.err(473): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-13 01:30:16.099: WARN/System.err(473): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
12-13 01:30:16.108: WARN/System.err(473): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-13 01:30:16.118: WARN/System.err(473): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-13 01:30:16.118: WARN/System.err(473): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-13 01:30:16.118: WARN/System.err(473): at com.textalert.alertCollection.getAlerts(alertCollection.java:46)
12-13 01:30:16.118: WARN/System.err(473): at com.textalert.alertsList.run(alertsList.java:81)
12-13 01:30:16.128: WARN/System.err(473): at java.lang.Thread.run(Thread.java:1096)
Code is
代码是
HttpClient client = new DefaultHttpClient();
String postURL = "http://textalertapp.com/androidCode/?api=AlertManager&method=getAlerts";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "kris"));
params.add(new BasicNameValuePair("pass", "xyz"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
回答by Ayaz Alavi
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I just added this line in manifest file. Problem solved
我刚刚在清单文件中添加了这一行。问题解决了
回答by AlexR
This means that your host is unknown, i.e. does not exist or is not accessible. Check you IP again and check whether you can get this IP from your device. Probably this IP exists in your local network and is not accessible from outside.
这意味着您的主机未知,即不存在或不可访问。再次检查您的 IP 并检查您是否可以从您的设备获取此 IP。可能此 IP 存在于您的本地网络中,并且无法从外部访问。
If IP exists check firewall definitions. Firewall cause the same effect.
如果 IP 存在,请检查防火墙定义。防火墙造成同样的效果。
回答by Chris Lacy
I just started receiving the "java.net.UnknownHostException" error when fetching content from a URL that previously worked perfectly.
当我从以前完美运行的 URL 获取内容时,我刚刚开始收到“java.net.UnknownHostException”错误。
After going around in circles for a while, I manually deleted my project's /bin folder and cleaned the project in Eclipse (Project -> Clean), which fixed this error.
转了一圈后,我手动删除了我项目的 /bin 文件夹,并在 Eclipse 中清理了项目(项目 -> 清理),修复了这个错误。