当前位置:操作系统 > 安卓/Android >>

android为HttpClient和HttpURLConnection添加中国移动代理

 

在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络

     当我们使用wap网络的时候,程序中必须要中国移动代理!这样的话,手机才能正常的访问internet!

     在android中,有两种方式请求网络:HttpURLConnection和HttpClient请求方式,如果网络状态为wap的时候,都要为两种请求添加中国移动代理的!

     第一种方式:HttpURLConnection

    

 

 

/**

 * @author sky

 * Email vipa1888@163.com

 * QQ:840950105

 * 使用HttpURLConnection请求Internet

 * @param context   context对象

 * @param requestUrl  请求的URL

 * @param param   请求的参数

 * @return  返回一个inputstream流

 */ 

public static InputStream getHttpURLConnectionInputStream(Context context,String requestUrl,Map<String, String> param) { 

     

    URL url; 

    HttpURLConnection conn = null; 

    InputStream input = null; 

    try { 

        url = new URL(requestUrl); 

        if(getAPNType(context)==NetWorkUtil.CMWAP)   //当请求的网络为wap的时候,就需要添加中国移动代理 

        { 

            Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("10.0.0.172", 80)); 

            conn = (HttpURLConnection) url.openConnection(proxy); 

        } 

            conn = (HttpURLConnection) url.openConnection(); 

         

            conn.setConnectTimeout(10000);    //请求超时 

            conn.setRequestMethod("POST");  //请求方式 

            conn.setReadTimeout(1000);   //读取超时 

            conn.setDoOutput(true); 

            conn.setDoInput(true); 

            conn.setUseCaches(false); 

            conn.setRequestProperty("Charset", "UTF-8"); 

            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 

            OutputStream os = conn.getOutputStream();     

            StringBuilder sb = new StringBuilder(); 

            Iterator<String> it = param.keySet().iterator(); 

            while (it.hasNext()) { 

                String key = it.next(); 

                String value = param.get(key); 

                sb.append(key).append("=").append(value).append("&"); 

            } 

            String p = sb.toString().substring(0, sb.length()-1); 

            System.out.println("请求的参数"+p); 

            os.write(p.getBytes("utf-8")); 

            os.close(); 

            if(conn!=null) 

            { 

                input = conn.getInputStream(); 

            } 

         

    } catch (Exception e) { 

        e.printStackTrace(); 

    } 

    return input; 

 

 

 

上面这种方式就是HttpURLConnection ,这种方式在android开发中也是比较常用的,希望朋友们也要熟悉的掌握!

       第二种方式:HttpClient

 

/**

 * @author sky

 * Email vipa1888@163.com

 * QQ:840950105

 * 使用HttpURLConnection请求Internet

 * @param context   context对象

 * @param requestUrl  请求的URL

 * @param param   请求的参数

 * @return  返回一个inputstream流

 */ 

public static InputStream getHttpClientInputStream(Context context,String requestUrl, Map<String, String> param)throws Exception { 

    HttpClient client = new DefaultHttpClient(); 

    if(getAPNType(context)==NetWorkUtil.CMWAP)  //当请求的网络为wap的时候,就需要添加中国移动代理 

    {  

        HttpHost proxy = new HttpHost("10.0.0.172", 80); 

        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 

                proxy); 

    }  www.zzzyk.com

    HttpPost hp = new HttpPost(requestUrl); 

    hp.setHeader("Charset", "UTF-8"); 

    hp.setHeader("Content-Type", "application/x-www-form-urlencoded"); 

    List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); 

     

    Iterator<String> it = param.keySet().iterator(); 

    while (it.hasNext()) { 

        String key = it.next(); 

        list.add(new BasicNameValuePair(key, param.get(key))); 

    } 

    hp.setEntity(new UrlEncodedFormEntity(list,"UTF-8")); 

    HttpResponse response = null; 

    response = client.execute(hp); 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,