我正在使用运行 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);
}
但代理地址和端口始终为空。
有人可以帮忙吗
PS。我在 Android 4.1/4.2/4.3 设备上绝对没问题