Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix this UnknownHostException?

Tags:

java

android

url

public static final String readURL(String url)throws Throwable
{
        try {
            InputStream in = (InputStream) fetch(url);
            byte[] bArr = readBytes(in);
            return new String(bArr);
        } catch (Throwable e) {
            throw e;
            }
}


public static final Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

I am behind a proxy and when I try

readURL("http://abc.com")

to access URL http://abc.com it throws java.net.UnknownHostException: I have:

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

in manifest file.

Any quick solutions?

like image 343
d-man Avatar asked Dec 03 '09 06:12

d-man


People also ask

How do you resolve UnknownHostException?

To resolve application level issues, try the following methods: Restart your application. Confirm that your Java application doesn't have a bad DNS cache. If possible, configure your application to adhere to the DNS TTL.


2 Answers

Proxy proxy = new Proxy(Proxy.DIRECT,
    new InetSocketAddress(proxyHost, proxyPort));
url.openConnection(proxy);

or

System.setProperty("http.proxyHost", "my.proxyhost.com");
System.setProperty("http.proxyPort", "1234");
like image 84
Bozho Avatar answered Sep 29 '22 10:09

Bozho


If you are running your app on Emulator then do the following-

1)Close all running emulator.

2)Clean your project and Close Eclipse.

3)Start the eclipse again.

4)Create new emulator and run your project.

Thats it! worked for me!!!

like image 24
dd619 Avatar answered Sep 29 '22 11:09

dd619