詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
Android應(yīng)用開發(fā)中,會(huì)經(jīng)常要提交數(shù)據(jù)到服務(wù)器和從服務(wù)器得到數(shù)據(jù),本文主要是給出了利用http協(xié)議采用HttpClient方式向服務(wù)器提交數(shù)據(jù)的方法。
代碼比較簡(jiǎn)單,這里不去過(guò)多的闡述,直接看代碼。
/** * @author Dylan * 本類封裝了Android中向web服務(wù)器提交數(shù)據(jù)的兩種方式四種方法 */ public class SubmitDataByHttpClientAndOrdinaryWay { /** * 使用get請(qǐng)求以普通方式提交數(shù)據(jù) * @param map 傳遞進(jìn)來(lái)的數(shù)據(jù),以map的形式進(jìn)行了封裝 * @param path 要求服務(wù)器servlet的地址 * @return 返回的boolean類型的參數(shù) * @throws Exception */ public Boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception { // 拼湊出請(qǐng)求地址 StringBuilder sb = new StringBuilder(path); sb.append("?"); for (Map.Entry<String, String> entry : map.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); String str = sb.toString(); System.out.println(str); URL Url = new URL(str); HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection(); HttpConn.setRequestMethod("GET"); HttpConn.setReadTimeout(5000); // GET方式的請(qǐng)求不用設(shè)置什么DoOutPut()之類的嗎? if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * 普通方式的DoPost請(qǐng)求提交數(shù)據(jù) * @param map 傳遞進(jìn)來(lái)的數(shù)據(jù),以map的形式進(jìn)行了封裝 * @param path 要求服務(wù)器servlet的地址 * @return 返回的boolean類型的參數(shù) * @throws Exception */ public Boolean submitDataByDoPost(Map<String, String> map, String path) throws Exception { // 注意Post地址中是不帶參數(shù)的,所以newURL的時(shí)候要注意不能加上后面的參數(shù) URL Url = new URL(path); // Post方式提交的時(shí)候參數(shù)和URL是分開提交的,參數(shù)形式是這樣子的:name=y&age=6 StringBuilder sb = new StringBuilder(); // sb.append("?"); for (Map.Entry<String, String> entry : map.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); String str = sb.toString(); HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection(); HttpConn.setRequestMethod("POST"); HttpConn.setReadTimeout(5000); HttpConn.setDoOutput(true); HttpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); HttpConn.setRequestProperty("Content-Length", String.valueOf(str.getBytes().length)); OutputStream os = HttpConn.getOutputStream(); os.write(str.getBytes()); if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * 以HttpClient的DoGet方式向服務(wù)器發(fā)送請(qǐng)數(shù)據(jù) * @param map 傳遞進(jìn)來(lái)的數(shù)據(jù),以map的形式進(jìn)行了封裝 * @param path 要求服務(wù)器servlet的地址 * @return 返回的boolean類型的參數(shù) * @throws Exception */ public Boolean submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception { HttpClient hc = new DefaultHttpClient(); // 請(qǐng)求路徑 StringBuilder sb = new StringBuilder(path); sb.append("?"); for (Map.Entry<String, String> entry : map.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); String str = sb.toString(); System.out.println(str); HttpGet request = new HttpGet(sb.toString()); HttpResponse response = hc.execute(request); if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * 以HttpClient的DoPost方式提交數(shù)據(jù)到服務(wù)器 * @param map 傳遞進(jìn)來(lái)的數(shù)據(jù),以map的形式進(jìn)行了封裝 * @param path 要求服務(wù)器servlet的地址 * @return 返回的boolean類型的參數(shù) * @throws Exception */ public Boolean submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception { // 1. 獲得一個(gè)相當(dāng)于瀏覽器對(duì)象HttpClient,使用這個(gè)接口的實(shí)現(xiàn)類來(lái)創(chuàng)建對(duì)象,DefaultHttpClient HttpClient hc = new DefaultHttpClient(); // DoPost方式請(qǐng)求的時(shí)候設(shè)置請(qǐng)求,關(guān)鍵是路徑 HttpPost request = new HttpPost(path); // 2. 為請(qǐng)求設(shè)置請(qǐng)求參數(shù),也即是將要上傳到web服務(wù)器上的參數(shù) List<NameValuePair> parameters = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : map.entrySet()) { NameValuePair nameValuePairs = new BasicNameValuePair(entry.getKey(), entry.getValue()); parameters.add(nameValuePairs); } // 請(qǐng)求實(shí)體HttpEntity也是一個(gè)接口,我們用它的實(shí)現(xiàn)類UrlEncodedFormEntity來(lái)創(chuàng)建對(duì)象,注意后面一個(gè)String類型的參數(shù)是用來(lái)指定編碼的 HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8"); request.setEntity(entity); // 3. 執(zhí)行請(qǐng)求 HttpResponse response = hc.execute(request); // 4. 通過(guò)返回碼來(lái)判斷請(qǐng)求成功與否 if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- CentOS服務(wù)器apache綁定多個(gè)域名的方法
- 騰訊云(ubuntu)下安裝 nodejs + 實(shí)現(xiàn) Nginx 反向代理服務(wù)器
- 在同一臺(tái)服務(wù)器上配置多個(gè)Tomcat的方法
- CentOS 7.2部署郵件服務(wù)器(Postfix)
- SVN 安裝教程之服務(wù)器和客戶端
- mysql自動(dòng)定時(shí)備份數(shù)據(jù)庫(kù)的最佳方法(windows服務(wù)器)
- Python 搭建Web站點(diǎn)之Web服務(wù)器與Web框架
- Nginx 服務(wù)器安裝及配置文件詳解介紹
- 服務(wù)器數(shù)據(jù)庫(kù)編碼格式問(wèn)題解決方案
相關(guān)文章
Android基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽
本篇文章主要介紹了基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08android中SwipeRefresh實(shí)現(xiàn)各種上拉,下拉刷新示例
這篇文章主要介紹了android中SwipeRefresh實(shí)現(xiàn)各種上拉,下拉刷新示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03Android Metro菜單實(shí)現(xiàn)思路及代碼
在安卓平臺(tái)上實(shí)現(xiàn)一下Metro菜單效果,之前有介紹過(guò)了,相信大家對(duì)此不會(huì)陌生了吧,感興趣的朋友可以了解下哈2013-06-06Android 帶有彈出收縮動(dòng)畫的扇形菜單實(shí)例
本篇文章主要介紹了Android 帶有彈出收縮動(dòng)畫的扇形菜單實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06Android 圓角邊框的實(shí)現(xiàn)方式匯總
這篇文章主要介紹了Android 圓角邊框的實(shí)現(xiàn)方式匯總的相關(guān)資料,需要的朋友可以參考下2016-03-03Activity isFinishing()判斷Activity的狀態(tài)實(shí)例
下面小編就為大家分享一篇Activity isFinishing()判斷Activity的狀態(tài)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03