gpt4 book ai didi

java - 调用 HttpGet 类的 execute() 方法时出现 IO 异常

转载 作者:行者123 更新时间:2023-11-30 04:47:25 25 4
gpt4 key购买 nike

为什么我在这段安静的代码中得到了 IOException?谢谢。

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com/");
HttpResponse response;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
Toast.makeText(this, "ClientProtocolEx", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "IOEx", Toast.LENGTH_LONG).show();
e.printStackTrace();
}

异常(exception):

12-25 18:01:48.992: WARN/System.err(10749): java.net.UnknownHostException: www.google.com
12-25 18:01:48.992: WARN/System.err(10749): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
12-25 18:01:48.992: WARN/System.err(10749): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
12-25 18:01:48.992: WARN/System.err(10749): at java.net.InetAddress.getAllByName(InetAddress.java:242)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-25 18:01:48.992: WARN/System.err(10749): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

最佳答案

HttpEntity entity = response.getEntity();
String s = entity.toString();

尝试在response = httpclient.execute(httpget);

之后添加这个

而且如果这没有帮助,您必须编辑您的 list xml 文件并授予您的应用程序 INTERNET 使用权限。

关于java - 调用 HttpGet 类的 execute() 方法时出现 IO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4530672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com