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

 

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

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

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

 

	/**
	 * @author spring sky
	 * Email vipa1888@163.com
	 * QQ:840950105 My name:石明政
	 * 使用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);
			}else{
   				 conn = 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 spring sky
	 * Email vipa1888@163.com
	 * QQ:840950105 My name :石明政
	 * 使用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);
		}
		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);
		return response.getEntity().getContent();
	}

 

这个httpClient实现了android内置的DefaultHttpClient,所以使用起来还是很方便的!

但是我发现HttpClient 比HttpURLConnection 要好一些,因为HttpURLConnection 如果使用wap在上网请求的时候,存在很多问题的(我是深有体会的,比如请求无响应,信号不好都可能造成一些未知的错误)

     好了,熟悉掌握了两种请求方式了,android的联网应用就可以开发了!呵呵 微笑

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值