项目中可能会调用到一些墙外的api,例如谷歌、youtube等等的。这时候就需要给项目配置代理了。

VM option

调试的时候在这里面配置,打包后运行的时候直接在命令行上加参数。

VM option.png

分别是http代理和socks代理。

代码

// http
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "10808");
System.setProperty("https.proxySet", "true");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "10808");
// socks
System.setProperty("socksProxyHost", "127.0.0.1");
System.setProperty("socksProxyPort", "10808");

代码放到合适的位置即可,当然可以通过配置文件或是别的方式动态设置代理信息。