0

我得到了这个例外:

org.apache.http.conn.HttpHostConnectException: Connection to http://www.google.es refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at sdf.main(sdf.java:17)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
... 8 more

我正在运行的代码是这个:

public class sdf {

static String url = "http://www.google.es";

public static void main(String[] args) {

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new DefaultHttpClient().execute(new HttpGet(url))
                        .getEntity().getContent()));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

url 的值是什么并不重要,它不会起作用。

我究竟做错了什么?

4

1 回答 1

1

您是否拥有经过正确许可的清单?这是你必须做的一个例子:

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

没有这个,您将无法连接互联网

于 2012-07-03T08:16:09.930 回答