標籤:
- /**
- * 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);
- 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;
- }
- }
原文出自 http://luecsc.blog.51cto.com/2219432/1111923
Android發送資料到web伺服器4種方式