android為HttpClient和HttpURLConnection添加中國移動代理

來源:互聯網
上載者:User

 

在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.2cto.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); 

    return response.getEntity().getContent(); 

 

這個httpClient實現了android內建的DefaultHttpClient,所以使用起來還是很方便的!

但是我發現HttpClient 比HttpURLConnection 要好一些,因為HttpURLConnection 如果使用wap在上網請求的時候,存在很多問題的(我是深有體會的,比如請求無響應,訊號不好都可能造成一些未知的錯誤)

     好了,熟悉掌握了兩種請求方式了,android的連網應用就可以開發了!呵呵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.