我正在尝试通过PC上的Android模拟器访问RESTful Web服务,该模拟器使用代理连接到互联网。
我有正常的代码可以在具有自己的数据连接的实际Android设备上访问Web服务,并使用以下代码:
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("username", "password");
request.addHeader(BasicScheme.authenticate(creds, "UTF-8", false));
HttpResponse response = client.execute(request);
我尝试过多种方法试图让Emulator允许通过代理连接,但没有一种方法有效。
注意,我确实在AndroidManifest.xml中启用了INTERNET:
<uses-permission android:name="android.permission.INTERNET" />
尝试1 - 设置属性:
这会在execute()调用
中为我的服务的URL生成UnknownHostExceptionProperties props = System.getProperties();
props.put("http.proxyHost", "httpproxy.mycompany.com");
props.put("http.proxyPort", "80");
尝试2 - 在DefaultHttpClient中设置代理:
这会为实际代理
生成UnknownHostExceptionDefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
HttpHost proxy = new HttpHost("httpproxy.mycompany.com", 80);
client.getCredentialsProvider().setCredentials(
new AuthScope("httpproxy.mycompany.com", 80),
new UsernamePasswordCredentials("username", "password"));
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials("username", "password");
request.addHeader(BasicScheme.authenticate(cred, "UTF-8", false));
HttpResponse response = client.execute(request);
尝试3 - 在HttpGet中设置代理
这会在我的HttpGet
中为URL产生UnknownHostExceptionDefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials("username", "password");
request.addHeader(BasicScheme.authenticate(cred, "UTF-8", false));
Header bs = new BasicScheme().authenticate(
new UsernamePasswordCredentials("username", "password"),
request);
request.addHeader("Proxy-Authorization", bs.getValue());
HttpResponse response = client.execute(request);
我不确定还有什么可以尝试的。我愿意接受任何建议。
答案 0 :(得分:1)
遇到同样的问题,我成功了尝试3的变体(下面的代码),最重要的区别是setProperty语句。请注意,我正在调用的Web服务不需要身份验证,因此我只设置代理授权标头。
System.setProperty("java.net.useSystemProxies", "false");
System.setProperty("http.proxyHost", "123.56.7.9");
System.setProperty("http.proxyPort", "8080");
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
HttpGet request = new HttpGet("web service url");
Header bs = new BasicScheme().authenticate(
new UsernamePasswordCredentials("NETWORKID", "netpassword"),
request);
request.addHeader("Proxy-Authorization", bs.getValue());
HttpResponse response = client.execute(request);
答案 1 :(得分:0)
你使用-http-proxy http://:emulator命令行选项还是“设置” - &gt; “无线和网络” - &gt; “移动网络” - &gt; “接入点名称” - &gt; “Telkila”或Home&gt;菜单&gt;设置&gt;无线控制&gt;移动网络&gt;接入点名称?