HTTP代理不能在HTC上运行android 2.2

时间:2013-02-28 15:05:51

标签: android http proxy

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

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

这适用于几个Android设备,都运行Android 2.3及更高版本。但是,当我尝试运行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)

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

1 个答案:

答案 0 :(得分: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.