Android聯網主要使用HttpURLConneciton和HttpClient進行聯網,在手機聯網的時候,我們優先選擇wifi網絡,其次在選擇移動網絡,這里所述移動網絡主要指cmwap。
大家都知道cmwap連接需要設置代理地址和端口,那么,android程序中如何設置代理呢?這是個問題。
HttpURLConnection設置代理
1 //當我們使用的是中國移動的手機網絡時,下面方法可以直接獲取得到10.0.0.172,80端口 2 String host=android.net.Proxy.getDefaultHost();//通過andorid.net.Proxy可以獲取默認的代理地址 3 int port =android.net.Proxy.getDefaultPort();//通過andorid.net.Proxy可以獲取默認的代理端口 4 SocketAddress sa=new InetSocketAddress(host,port); 5 //定義代理,此處的Proxy是源自java.net 6 Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,sa); 7 URL getUrl = new URL(“www.baidu.com”); 8 HttpURLConnection con = (HttpURLConnection) getUrl.openConnection(proxy);//設置代理
HttpClient設置代理
1 DefaultHttpClient httpClient=new DefaultHttpClient(); 2 String host=Proxy.getDefaultHost();//此處Proxy源自android.net 3 int port = Proxy.getPort(context);//同上 4 HttpHost httpHost = new HttpHost(host, port); 5 //設置代理 6 httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost); 7 HttpGet httpGet=new HttpPost("<a href="http://www.baidu.com">www.baidu.com</a>"); 8 HttpResponse response=httpClient.execute(httpGet);
第一種方式:通過HttpURLConnection來訪問
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
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在上網請求的時候,存在很多問題的(我是深有體會的,比如請求無響應,信號不好都可能造成一些未知的錯誤).