在模拟器中设置了 代理 后 程序中依旧不能访问internet数据, 解决办法
Properties prop = System.getProperties();
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", proxyHost);
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", String.valueOf(proxyPort));
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator(proxyUser, proxyPass));
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
并且在AndroidManifest.xml 添加 <uses-permission android:name="android.permission.INTERNET"/>