Michael's Blog

Michael's Blog Space

 

android emulator 通过代理访问web service

1.启动模拟器(android 2.2 emulator ),然后进入 settings->Wireless & network ->Mobile networks->Access Point Names   

然后打开出现在列表中的 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);
        }
    }
}

 

原文链接:http://blog.csdn.net/fhy_2008/article/details/7027892

posted on 2012-07-06 12:04  Michael.Wang  阅读(3797)  评论(0编辑  收藏  举报

导航