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

时间:2010-12-25 15:44:19

标签: java android httpclient

为什么我在这段代码中获得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();
    } 

例外:

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)

1 个答案:

答案 0 :(得分:1)

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

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

之后添加此内容

如果这没有帮助,您必须编辑清单xml文件并为您的应用程序提供INTERNET使用权限。