国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解Android提交數據到服務器的兩種方式四種方法

2019-12-12 04:47:30
字體:
來源:轉載
供稿:網友

Android應用開發中,會經常要提交數據到服務器和從服務器得到數據,本文主要是給出了利用http協議采用HttpClient方式向服務器提交數據的方法。

代碼比較簡單,這里不去過多的闡述,直接看代碼。

/** * @author Dylan * 本類封裝了Android中向web服務器提交數據的兩種方式四種方法 */public class SubmitDataByHttpClientAndOrdinaryWay { /** * 使用get請求以普通方式提交數據 * @param map 傳遞進來的數據,以map的形式進行了封裝 * @param path 要求服務器servlet的地址 * @return 返回的boolean類型的參數 * @throws Exception */ public Boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception { // 拼湊出請求地址 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方式的請求不用設置什么DoOutPut()之類的嗎? if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {  return true; } return false; } /** * 普通方式的DoPost請求提交數據 * @param map 傳遞進來的數據,以map的形式進行了封裝 * @param path 要求服務器servlet的地址 * @return 返回的boolean類型的參數 * @throws Exception */ public Boolean submitDataByDoPost(Map<String, String> map, String path) throws Exception { // 注意Post地址中是不帶參數的,所以newURL的時候要注意不能加上后面的參數 URL Url = new URL(path); // Post方式提交的時候參數和URL是分開提交的,參數形式是這樣子的: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方式向服務器發送請數據 * @param map 傳遞進來的數據,以map的形式進行了封裝 * @param path 要求服務器servlet的地址 * @return 返回的boolean類型的參數 * @throws Exception */ public Boolean submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception { HttpClient hc = new DefaultHttpClient(); // 請求路徑 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方式提交數據到服務器 * @param map 傳遞進來的數據,以map的形式進行了封裝 * @param path 要求服務器servlet的地址 * @return 返回的boolean類型的參數 * @throws Exception */ public Boolean submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception { // 1. 獲得一個相當于瀏覽器對象HttpClient,使用這個接口的實現類來創建對象,DefaultHttpClient HttpClient hc = new DefaultHttpClient(); // DoPost方式請求的時候設置請求,關鍵是路徑 HttpPost request = new HttpPost(path); // 2. 為請求設置請求參數,也即是將要上傳到web服務器上的參數 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); } // 請求實體HttpEntity也是一個接口,我們用它的實現類UrlEncodedFormEntity來創建對象,注意后面一個String類型的參數是用來指定編碼的 HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8"); request.setEntity(entity); // 3. 執行請求 HttpResponse response = hc.execute(request); // 4. 通過返回碼來判斷請求成功與否 if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {  return true; } return false; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭阳县| 纳雍县| 乐亭县| 安庆市| 建瓯市| 昌图县| 内丘县| 新野县| 太仆寺旗| 清流县| 九台市| 旌德县| 广州市| 吴川市| 茂名市| 应城市| 政和县| 湖北省| 定陶县| 黄浦区| 张家港市| 永仁县| 乳山市| 公主岭市| 怀仁县| 宁阳县| 奉节县| 海宁市| 伊川县| 应城市| 曲阜市| 寻乌县| 肥西县| 丰城市| 榆树市| 日喀则市| 安龙县| 陇川县| 西贡区| 吉林市| 阳泉市|