GET方式
實驗:提交視頻名稱、時長信息
url:
http://192.168.1.102:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90
資源
- <string name="app_name">VideoClient</string>
-
- <string name="name">視頻名稱</string>
-
- <string name="timeLength">時長</string>
-
- <string name="save">保存</string>
-
- <string name="success">數據保存成功</string>
-
- <string name="fail">數據保存失敗</string>
-
- <string name="error">網絡連接失敗</string>
-
- <string name="videoFile">視頻文件</string>
布局
- <TextView
-
- Android:layout_width="fill_parent"
-
- android:layout_height="wrap_content"
-
- android:text="@string/name" />
-
-
-
- <EditText
-
- android:id="@+id/nameEt"
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content" >
-
-
-
- <requestFocus />
-
- </EditText>
-
-
-
- <TextView
-
- android:layout_width="fill_parent"
-
- android:layout_height="wrap_content"
-
- android:text="@string/timeLength" />
-
-
-
- <EditText
-
- android:id="@+id/timeLengthEt"
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content" >
-
- </EditText>
-
-
-
- <Button
-
- android:id="@+id/saveBtn"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="save" />
VideoService
- public static boolean save(String name, String timeLength) throws Exception {
-
- String path = "http://192.168.1.102:8080/videoweb/video/manage.do";
-
- Map<String, String> params = new HashMap<String, String>();
-
- params.put("name", name);
-
- params.put("timelength", timeLength);
-
- params.put("method", "save");
-
-
-
- return sendPostRequestHttpClient(path, params, "UTF-8");
-
- }
-
-
-
- private static boolean sendPostRequestHttpClient(String path,
-
- Map<String, String> params, String string) throws Exception {
-
- // http://192.168.1.102:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90
-
- StringBuilder pathBuilder = new StringBuilder(path);
-
- pathBuilder.append("?");
-
-
-
- for (Map.Entry<String, String> entry : params.entrySet()) {
-
- pathBuilder.append(entry.getKey()).append("=")
-
- .append(entry.getValue()).append("&");
-
- }
-
- pathBuilder.deleteCharAt(pathBuilder.length() - 1);
-
-
-
- URL url = new URL(pathBuilder.toString());
-
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setRequestMethod("GET");
-
- conn.setConnectTimeout(5000);
-
- if (conn.getResponseCode() == 200) {
-
- return true;
-
- }
-
- return false;
-
- }
測試方法代碼
VideoServiceTest
- public void testSave() throws Throwable{
-
- VideoService.save("Sucan", "80");
-
- }
中文字符解決方案:
服務器端代碼:
- VideoForm formbean = (VideoForm)form;
-
- if("GET".equals(request.getMethod())){
-
- String name = new String(request.getParameter("name")
-
- .getBytes("ISO8859-1"),"UTF-8");
-
- System.out.println("視頻名稱: "+ name);
-
- System.out.println("時長: "+formbean.getTimelength());
-
- }else{
-
- System.out.println("視頻名稱: "+formbean.getName());
-
- System.out.println("時長: "+formbean.getTimelength());
-
- }
客戶端的VideoService中
POST方式
修改服務器端代碼
Index.jsp
- <form action="http://192.168.1.102:8080/videoweb/video/manage.do"
-
- method="post" >
-
- <input type="hidden" name="method" value="save"><br/>
-
- 視頻名稱:<input type="text" name="name" value=""><br/>
-
- 時長:<input type="text" name="timelength" value=""><br/>
-
-
-
- <input type="submit" value="提交">
-
- </form>
利用HttpWatch對post傳輸進行簡單講解:
- POST /videoweb/video/manage.do HTTP/1.1
-
- Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
-
- Referer: http://192.168.1.102:8080/videoweb/
-
- Accept-Language: zh-cn,en-GB;q=0.5
-
- User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
-
- Content-Type: application/x-www-form-urlencoded
-
- Accept-Encoding: gzip, deflate
-
- Host: 192.168.1.102:8080
-
- Content-Length: 34
-
- Connection: Keep-Alive
-
- Cache-Control: no-cache
-
- Cookie: JSESSIONID=AE51607F874103B085624AD38C68CF56
-
-
-
- method=save&name=xxxx&timelength=2
編寫業務類方法(VideoService)
- private static boolean sendPostRequestHttpClient(String path,
-
- Map<String, String> params, String encoding) throws Exception {
-
- /*
-
- * Content-Type: application/x-www-form-urlencoded
-
- * Content-Length: 34
-
- * method=save&name=xxxx&timelength=2
-
- */
-
- StringBuffer sb = new StringBuffer("");
-
- if (params != null && !params.isEmpty()) {
-
- for (Map.Entry<String, String> entry : params.entrySet()) {
-
- sb.append(entry.getKey()).append("=")
-
- // .append(entry.getValue()).append("&");
-
- .append(URLEncoder.encode(entry.getValue(), encoding)).append('&'); //url編碼,解決中文亂碼
-
- }
-
- sb.deleteCharAt(sb.length() - 1);
-
- }
-
-
-
- byte[] data = sb.toString().getBytes(); //得到實體數據eg. method=save&name=xxxx&timelength=2
-
- URL url = new URL(path);
-
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setRequestMethod("POST");
-
- conn.setConnectTimeout(5000);
-
- conn.setDoOutput(true); //允許通過POST方式提交數據,必須允許輸出
-
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
-
- conn.setRequestProperty("Content-Length", String.valueOf(data.length));
-
-
-
- OutputStream outStream = conn.getOutputStream();
-
- outStream.write(data);
-
- outStream.flush();
-
- outStream.close();
-
-
-
- if (conn.getResponseCode() == 200) {
-
- return true;
-
- }
-
- return false;
-
- }
修改save方法
- // return sendGetRequestHttpClient(path, params, "UTF-8");
-
- return sendPostRequest(path, params, "UTF-8");