How do I fix this UnknownHostException?

2019-06-22 01:49发布

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?

标签: java android url
4条回答
唯我独甜
2楼-- · 2019-06-22 02:08
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");
查看更多
Anthone
3楼-- · 2019-06-22 02:16

It is possible that fetch does not do the necessary DNS lookups. If you nslookup abc.com you get a redirect to www.go.com. This may be the problem.

查看更多
Animai°情兽
4楼-- · 2019-06-22 02:22

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!!!

查看更多
相关推荐>>
5楼-- · 2019-06-22 02:27

Try using this, if you are behind the proxy and firewall. The reason is your IE and FF is enabled with proxy setting but emulator is not yet configured.

  1. adb shell

  2. sqlite3 /data/data/com.google.android.providers.settings/databases/settings.db

  3. sqlite> INSERT INTO system VALUES(99,’http_proxy', 'proxy:port');
  4. sqlite> SELECT * FROM system;

You should be able to see the last inserted value at bottom of your table.

Browse thro this link : Proxy which requires authentication with Android Emulator

查看更多
登录 后发表回答