android emulator 通过代理访问web service
来源:互联网 发布:java框架 与 .net区别 编辑:程序博客网 时间:2024/04/30 15:58
然后打开出现在列表中的 Telkila.
2.然后下面这样设置:
- Proxy : your proxy address
- Port : your proxy port
经过以上2步,系统的浏览器已经可以访问internet了。但是要让程序中的webview或httpconnection等访问internet还不行。怎么办呢?
首先,要让程序访问internet,需要加permission, 第二个permission为添加代理而增加:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1。 对于webview
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings()
.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.enablePlatformNotifications();
搞定!
2. 对于URLConnection:
URL url = new URL(urlStr);
String host = android.net.Proxy.getDefaultHost();
int port = android.net.Proxy.getDefaultPort();
SocketAddress sa = new InetSocketAddress(host, port);
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
sa);
URLConnection conn = url.openConnection(proxy);
InputStream is = conn.getInputStream();
搞定!
3. 对于HttpClient:
DefaultHttpClient httpClient=new DefaultHttpClient();
String host=Proxy.getDefaultHost();//此处Proxy源自android.net
int port = Proxy.getPort(context);//同上
HttpHost httpHost = new HttpHost(host, port);
//设置代理
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost);
HttpGet httpGet=new HttpPost("<a href="http://www.baidu.com">www.baidu.com</a>");
HttpResponse response=httpClient.execute(httpGet);
4. 以下为转载的相关内容:
判断手机是否联网
boolean isConnect(){
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
return true;
}
return false;
}
判断当前网络是否为WIFI
boolean isWifi(){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(!ni.getTypeName().equals("WIFI")){
/*
* ni.getTypeNmae()可能取值如下
* WIFI,表示WIFI联网
* MOBILE,表示GPRS、EGPRS
* 3G网络没有测试过
* WIFI和(E)GPRS不能共存,如果两个都打开,系统仅支持WIFI
*/
return true;
}
}
return false;
}
综合判断网络类型,我们便可以确定是否需要设置代理,实现正确的联网。
示例一
HttpURLConnection con =null;URL postUrl = new URL("www.baidu.com");
boolean isProxy=false;
//网络检测
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isProxy=false;
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(! ni.getTypeName().equals("WIFI")){
isProxy=true;
}
}
}
if(isProxy){
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
con = (HttpURLConnection) postUrl.openConnection(proxy);
}else{
con = (HttpURLConnection) postUrl.openConnection();
}
示例二
DefaultHttpClient httpClient=new DefaultHttpClient();
//网络检测
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(!ni.getTypeName().equals("WIFI")){
//设置代理
String host=Proxy.getDefaultHost();
int port = Proxy.getPort(context);
HttpHost httpHost = new HttpHost(host, port);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost);
}
}
}
- android emulator 通过代理访问web service
- 通过代理访问web
- InfoPath 通过Web Service 访问Sql Server
- 通过SecureCRT设置代理,访问网络中web界面
- 通过Web Service在本地访问远程数据库的数据
- exchange web service:通过EWS访问Exchange收件箱
- 如何通过HTTPS(SSL加密)方式访问web service
- 如何通过HTTPS(SSL加密)方式访问web service
- 如何通过HTTPS(SSL加密)方式访问web service
- Web Service代理
- perl 通过代理 访问
- 通过url访问web----android 代码学习
- 使用代理访问web
- 如何访问web service
- vbscript访问web service
- java访问web service
- 浏览器访问web service
- gSoap访问Web Service
- 查找一个单元格内某个字符的个数(execel)
- 开发符合国际化标准的软件
- android 如何判断程序是否在前台运行
- 【转】虚拟列表
- THIS_MODULE介绍
- android emulator 通过代理访问web service
- jira4.4.3在ie9下运行的兼容性问题
- [下载].xx Mac OS X Lion [官方正式版]+[DMG]---种子■免费■下载
- sql server 2000 数据对象的最大容量说明
- C#打开文件
- 高效团队建设
- ECPSS 商户接口返回状态码
- 基于电子健康档案的区域医疗 SOA 解决方案
- 内存对齐,位段,大小端 .