我正在使用在Android 2.3.4上运行的三星Galaxy S Plus手机
我手动设置代理服务器和手机中的端口,在“设置” - >无线&网络 - > Wi-Fi设置 - > MENU按钮 - >高级面板。
我的手机仍然无法使用代理进行通信。我尝试了浏览器,但无法使用代理设置。
public static InputStream inputStreamForUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // doesn't work on android 2.3.4
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(30000);
urlConnection.setReadTimeout(30000);
System.out.println(urlConnection.usingProxy());
urlConnection.connect();
return urlConnection.getInputStream();
}
但是,如果我在代码中手动对代理进行硬编码,则可以正常工作
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.url", 8080));// this needs to be hard coded to make it work in 2.3
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(proxy);// this needs to be hard coded to make it work in 2.3
我试图动态找出系统代理,然后在代码中使用它,
private static void getProxySettings()
{
final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= 14;
String proxyAddress = "";
int proxyPort = 0;
if( IS_ICS_OR_LATER )
{
proxyAddress = System.getProperty( "http.proxyHost" );
String portStr = System.getProperty( "http.proxyPort" );
proxyPort = Integer.parseInt( ( portStr != null ? portStr : "-1" ) );
}
else
{
proxyAddress = android.net.Proxy.getHost( ctx );
proxyPort = android.net.Proxy.getPort( ctx );
}
System.out.println(proxyAddress);
System.out.println(proxyPort);
}
但代理地址和端口始终为null。
有人可以帮忙吗
PS。我在Android 4.1 / 4.2 / 4.3设备上完全没问题