访问URL时出现java.net.UnknownHostException

时间:2012-03-28 14:09:17

标签: java urlconnection

代码如下:

URL oracle = new URL("http://www.oracle.com/");   
URLConnection inputStream =oracle.openConnection();
InputStream in = inputStream.getInputStream();

引发此异常:

java.net.UnknownHostException: www.oracle.com
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
        at sun.net.www.http.HttpClient.New(HttpClient.java:307)
        at sun.net.www.http.HttpClient.New(HttpClient.java:324)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)

我认为抛出此异常是因为代理服务器阻止了我的连接?我只想解析URL中的一些数据,我可以在不进入防火墙的情况下完成此操作吗?

2 个答案:

答案 0 :(得分:3)

确保在清单中包含以下行。它应该是manifest节点的直接子节点。

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

答案 1 :(得分:2)

如果您使用的是代理服务器,请确保您正在设置相应的系统属性。 有关详细信息,请参阅http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html上的Oracle页面。