我正在使用在Android 2.3.4上运行的三星Galaxy S Plus手机
我在设置 - >无线和网络 - > Wi-Fi设置 - >菜单按钮 - >高级面板中手动设置手机中的代理服务器和端口 .
我的手机仍然无法使用代理进行通信 . 我试过浏览器,但是无法使用代理设置 .
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设备上完全没问题