Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.UnknownHostException when accessing a URL

Below code :

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

Throws this exception :

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)

I think this exception is thrown because the proxy server is blocking my connection? I just want to parse some data from the URL, can I accomplish this without running into the firewall ?

like image 824
blue-sky Avatar asked Sep 16 '26 01:09

blue-sky


2 Answers

If you are using a proxy server, please make sure that you are setting the appropriate system properties. See the Oracle page at http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html for details.

like image 141
Philip Helger Avatar answered Sep 17 '26 15:09

Philip Helger


Make sure to include the below line in your manifest. It should be a direct child of manifest node.

<uses-permission android:name="android.permission.INTERNET" />
like image 45
tony9099 Avatar answered Sep 17 '26 16:09

tony9099