0

我正在使用以下代码使用 HTTP 代理:

DefaultHttpClient client = new DefaultHttpClient();
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

这适用于几个运行 android 2.3 及更高版本的 android 设备。但是,当我在运行 android 2.2.2 的 HTC 愿望上尝试此操作时,出现以下异常:

java.lang.IllegalArgumentException
java.net.InetSocketAddress.<init>(InetSocketAddress.java:98)
java.net.InetSocketAddress.<init>(InetSocketAddress.java:89)
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.<init>(OpenSSLSocketImpl.java:238)
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImplWrapper.<init>(OpenSSLSocketImplWrapper.java:35)
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:83)
de.bahn.android.ris.communication.http.EasySSLSocketFactory.createSocket(EasySSLSocketFactory.java:136)
org.apache.http.impl.conn.DefaultClientConnectionOperator.updateSecureConnection(DefaultClientConnectionOperator.java:225)
org.apache.http.impl.conn.AbstractPoolEntry.layerProtocol(AbstractPoolEntry.java:302)
org.apache.http.impl.conn.AbstractPooledConnAdapter.layerProtocol(AbstractPooledConnAdapter.java:146)
org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:623)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:352)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

有没有人见过这种行为?谢谢!

4

1 回答 1

0

https://android-review.googlesource.com/#/c/15755/中所述,这似乎是在某个时候解决的错误,很可能在 2.2 之后。我在这里找到了解决方案http://code.google.com/p/android/issues/detail?id=2690,即替换

HttpResponse response = httpclient.execute(httpRequest);

int port = httpRequest.getURI().toString().startsWith("https") ? 443 : 80;
HttpHost host = new HttpHost(httpRequest.getURI().getHost(), port, httpRequest.getURI().getScheme());
HttpResponse response = httpclient.execute(host, httpRequest);

显然问题是端口设置不正确,在 InetSocketAddress 中设置为 0。

于 2013-03-01T13:55:05.807 回答