转自: http://www.2cto.com/kf/201111/112100.html
在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络
当我们使用wap网络的时候,程序中必须要中国移动代理!这样的话,手机才能正常的访问internet!
在android中,有两种方式请求网络:HttpURLConnection和HttpClient请求方式,如果网络状态为wap的时候,都要为两种请求添加中国移动代理的!
第一种方式:HttpURLConnection
1 /** 2 * @author sky 3 * 使用HttpURLConnection请求Internet 4 * @param context context对象 5 * @param requestUrl 请求的URL 6 * @param param 请求的参数 7 * @return 返回一个inputstream流 8 */ 9 public static InputStream getHttpURLConnectionInputStream(Context context, 10 String requestUrl, Map<String, String> param) { 11 URL url; 12 HttpURLConnection conn = null; 13 InputStream input = null; 14 try { 15 url = new URL(requestUrl); 16 if (getAPNType(context) == NetWorkUtil.CMWAP) // 当请求的网络为wap的时候,就需要添加中国移动代理 17 { 18 Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, 19 new InetSocketAddress("10.0.0.172", 80)); 20 conn = (HttpURLConnection) url.openConnection(proxy); 21 } 22 conn = (HttpURLConnection) url.openConnection(); 23 conn.setConnectTimeout(10000); // 请求超时 24 conn.setRequestMethod("POST"); // 请求方式 25 conn.setReadTimeout(1000); // 读取超时 26 conn.setDoOutput(true); 27 conn.setDoInput(true); 28 conn.setUseCaches(false); 29 conn.setRequestProperty("Charset", "UTF-8"); 30 conn.setRequestProperty("Content-Type", 31 "application/x-www-form-urlencoded"); 32 33 OutputStream os = conn.getOutputStream(); 34 StringBuilder sb = new StringBuilder(); 35 Iterator<String> it = param.keySet().iterator(); 36 while (it.hasNext()) { 37 String key = it.next(); 38 String value = param.get(key); 39 sb.append(key).append("=").append(value).append("&"); 40 } 41 String p = sb.toString().substring(0, sb.length() - 1); 42 System.out.println("请求的参数" + p); 43 os.write(p.getBytes("utf-8")); 44 os.close(); 45 if (conn != null){ 46 input = conn.getInputStream(); 47 } 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 return input; 52 }
上面这种方式就是HttpURLConnection ,这种方式在android开发中也是比较常用的,希望朋友们也要熟悉的掌握!
第二种方式:HttpClient
1 /** 2 * 3 * @author sky 4 * 5 * 使用HttpURLConnection请求Internet 6 * 7 * @param context 8 * context对象 9 * 10 * @param requestUrl 11 * 请求的URL 12 * 13 * @param param 14 * 请求的参数 15 * 16 * @return 返回一个inputstream流 17 */ 18 public static InputStream getHttpClientInputStream(Context context,String requestUrl, Map<String, String> param)
throws Exception { 19 HttpClient client = new DefaultHttpClient(); 20 if(getAPNType(context)==NetWorkUtil.CMWAP) //当请求的网络为wap的时候,就需要添加中国移动代理 21 { 22 HttpHost proxy = new HttpHost("10.0.0.172", 80); 23 client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 24 } 25 HttpPost hp = new HttpPost(requestUrl); 26 hp.setHeader("Charset", "UTF-8"); 27 hp.setHeader("Content-Type", "application/x-www-form-urlencoded"); 28 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); 29 Iterator<String> it = param.keySet().iterator(); 30 while (it.hasNext()) { 31 String key = it.next(); 32 list.add(new BasicNameValuePair(key, param.get(key))); 33 } 34 hp.setEntity(new UrlEncodedFormEntity(list,"UTF-8")); 35 HttpResponse response = null; 36 response = client.execute(hp); 37 return response.getEntity().getContent(); 38 }
这个httpClient实现了android内置的DefaultHttpClient,所以使用起来还是很方便的!
但是我发现HttpClient 比HttpURLConnection 要好一些,因为HttpURLConnection 如果使用wap在上网请求的时候,存在很多问题的(我是深有体会的,比如请求无响应,信号不好都可能造成一些未知的错误)