Android 開發工具類 28_sendGETRequest

來源:互聯網
上載者:User

標籤:

以 GET 方式上傳資料,小於 2K,且安全性要求不高的情況下。

 1 package com.wangjialin.internet.userInformation.service; 2  3 import java.net.HttpURLConnection; 4 import java.net.URL; 5 import java.net.URLEncoder; 6 import java.util.HashMap; 7 import java.util.Map; 8  9 public class UserInformationService {10     11     public static boolean save(String title, String length) throws Exception{12         13         String path = "http://192.168.1.103:8080/ServerForGETMethod/ServletForGETMethod";14         Map<String, String> params = new HashMap<String, String>();15         params.put("name", title);16         params.put("age", length);17         18         return sendGETRequest(path, params, "UTF-8");19     }    20     21     /**22      * 發送GET請求23      * @param path 請求路徑24      * @param params 請求參數25      * @return26      */27     private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception{28         // http://192.178.1.103:8080/ServerForGETMethod/ServletForGETMethod?title=xxxx&length=9029         StringBuilder sb = new StringBuilder(path);30         31         if(params != null && !params.isEmpty()){32             sb.append("?");33             for(Map.Entry<String, String> entry : params.entrySet()){34                 sb.append(entry.getKey()).append("=");35                 sb.append(URLEncoder.encode(entry.getValue(), encoding));36                 sb.append("&");37             }38             sb.deleteCharAt(sb.length() - 1);39         }40         HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection();41         conn.setConnectTimeout(5000);42         conn.setRequestMethod("GET");43         44         if(conn.getResponseCode() == 200){45             return true;46         }47         return false;48     }49 }

UserInformationActivity

 1 package com.wangjialin.internet.userInformation.get; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.EditText; 7 import android.widget.Toast; 8  9 import com.wangjialin.internet.userInformation.service.UserInformationService;10 11 public class UserInformationActivity extends Activity {12     13      private EditText titleText;14         private EditText lengthText;15         16         @Override17         public void onCreate(Bundle savedInstanceState) {18             super.onCreate(savedInstanceState);19             setContentView(R.layout.main);20             21             titleText = (EditText) this.findViewById(R.id.title);22             lengthText = (EditText) this.findViewById(R.id.length);23         }24         25         public void save(View v){26             String title = titleText.getText().toString();27             String length = lengthText.getText().toString();28             try {29                 boolean result = false;30                 31                 result = UserInformationService.save(title, length);32             33                 if(result){34                     Toast.makeText(this, R.string.success, 1).show();35                 }else{36                     Toast.makeText(this, R.string.fail, 1).show();37                 }38             } catch (Exception e) {39                 e.printStackTrace();40                 Toast.makeText(this, R.string.fail, 1).show();41             }42         }43 }

 

Android 開發工具類 28_sendGETRequest

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.