Java 如何修复此 UnknownHostException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1838140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:09:30  来源:igfitidea点击:

How do I fix this UnknownHostException?

javaandroidurl

提问by d-man

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")

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

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

访问 URL http://abc.com它抛出 java.net.UnknownHostException: 我有:

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

in manifest file.

在清单文件中。

Any quick solutions?

任何快速解决方案?

采纳答案by Bozho

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");

回答by Andrew Cox

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.

fetch 可能不执行必要的 DNS 查找。如果您 nslookup abc.com,您将重定向至 www.go.com。这可能是问题所在。

回答by ReachmeDroid

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.

如果您在代理和防火墙后面,请尝试使用它。原因是您的 IE 和 FF 启用了代理设置,但尚未配置模拟器。

  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;
  1. 亚行外壳

  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

通过此链接浏览:需要使用 Android Emulator 进行身份验证的代理

回答by dd619

If you are running your app on Emulatorthen do the following-

如果您在模拟器上运行您的应用程序,请执行以下操作 -

1)Close all running emulator.

1)关闭所有正在运行的模拟器。

2)Clean your project and Close Eclipse.

2)清理你的项目并关闭Eclipse。

3)Start the eclipse again.

3)再次启动eclipse。

4)Create new emulator and run your project.

4)创建新的模拟器并运行您的项目。

Thats it! worked for me!!!

就是这样!为我工作!!!