歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之Android平台向web應用提交信息

Android應用開發之Android平台向web應用提交信息

日期:2017/3/1 10:41:45   编辑:Linux編程

GET方式

實驗:提交視頻名稱、時長信息

url:

http://192.168.1.102:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90

資源

  1. <string name="app_name">VideoClient</string>
  2. <string name="name">視頻名稱</string>
  3. <string name="timeLength">時長</string>
  4. <string name="save">保存</string>
  5. <string name="success">數據保存成功</string>
  6. <string name="fail">數據保存失敗</string>
  7. <string name="error">網絡連接失敗</string>
  8. <string name="videoFile">視頻文件</string>

布局

  1. <TextView
  2. Android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:text="@string/name" />
  5. <EditText
  6. android:id="@+id/nameEt"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" >
  9. <requestFocus />
  10. </EditText>
  11. <TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="@string/timeLength" />
  15. <EditText
  16. android:id="@+id/timeLengthEt"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content" >
  19. </EditText>
  20. <Button
  21. android:id="@+id/saveBtn"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="save" />

VideoService

  1. public static boolean save(String name, String timeLength) throws Exception {
  2. String path = "http://192.168.1.102:8080/videoweb/video/manage.do";
  3. Map<String, String> params = new HashMap<String, String>();
  4. params.put("name", name);
  5. params.put("timelength", timeLength);
  6. params.put("method", "save");
  7. return sendPostRequestHttpClient(path, params, "UTF-8");
  8. }
  9. private static boolean sendPostRequestHttpClient(String path,
  10. Map<String, String> params, String string) throws Exception {
  11. // http://192.168.1.102:8080/videoweb/video/manage.do?method=save&name=xxx&timelength=90
  12. StringBuilder pathBuilder = new StringBuilder(path);
  13. pathBuilder.append("?");
  14. for (Map.Entry<String, String> entry : params.entrySet()) {
  15. pathBuilder.append(entry.getKey()).append("=")
  16. .append(entry.getValue()).append("&");
  17. }
  18. pathBuilder.deleteCharAt(pathBuilder.length() - 1);
  19. URL url = new URL(pathBuilder.toString());
  20. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  21. conn.setRequestMethod("GET");
  22. conn.setConnectTimeout(5000);
  23. if (conn.getResponseCode() == 200) {
  24. return true;
  25. }
  26. return false;
  27. }

測試方法代碼

VideoServiceTest

  1. public void testSave() throws Throwable{
  2. VideoService.save("Sucan", "80");
  3. }

中文字符解決方案:

服務器端代碼:

  1. VideoForm formbean = (VideoForm)form;
  2. if("GET".equals(request.getMethod())){
  3. String name = new String(request.getParameter("name")
  4. .getBytes("ISO8859-1"),"UTF-8");
  5. System.out.println("視頻名稱: "+ name);
  6. System.out.println("時長: "+formbean.getTimelength());
  7. }else{
  8. System.out.println("視頻名稱: "+formbean.getName());
  9. System.out.println("時長: "+formbean.getTimelength());
  10. }

客戶端的VideoService中

POST方式

修改服務器端代碼

Index.jsp

  1. <form action="http://192.168.1.102:8080/videoweb/video/manage.do"
  2. method="post" >
  3. <input type="hidden" name="method" value="save"><br/>
  4. 視頻名稱:<input type="text" name="name" value=""><br/>
  5. 時長:<input type="text" name="timelength" value=""><br/>
  6. <input type="submit" value="提交">
  7. </form>

利用HttpWatch對post傳輸進行簡單講解:

  1. POST /videoweb/video/manage.do HTTP/1.1
  2. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
  3. Referer: http://192.168.1.102:8080/videoweb/
  4. Accept-Language: zh-cn,en-GB;q=0.5
  5. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
  6. Content-Type: application/x-www-form-urlencoded
  7. Accept-Encoding: gzip, deflate
  8. Host: 192.168.1.102:8080
  9. Content-Length: 34
  10. Connection: Keep-Alive
  11. Cache-Control: no-cache
  12. Cookie: JSESSIONID=AE51607F874103B085624AD38C68CF56
  13. method=save&name=xxxx&timelength=2

編寫業務類方法(VideoService)

  1. private static boolean sendPostRequestHttpClient(String path,
  2. Map<String, String> params, String encoding) throws Exception {
  3. /*
  4. * Content-Type: application/x-www-form-urlencoded
  5. * Content-Length: 34
  6. * method=save&name=xxxx&timelength=2
  7. */
  8. StringBuffer sb = new StringBuffer("");
  9. if (params != null && !params.isEmpty()) {
  10. for (Map.Entry<String, String> entry : params.entrySet()) {
  11. sb.append(entry.getKey()).append("=")
  12. // .append(entry.getValue()).append("&");
  13. .append(URLEncoder.encode(entry.getValue(), encoding)).append('&'); //url編碼,解決中文亂碼
  14. }
  15. sb.deleteCharAt(sb.length() - 1);
  16. }
  17. byte[] data = sb.toString().getBytes(); //得到實體數據eg. method=save&name=xxxx&timelength=2
  18. URL url = new URL(path);
  19. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  20. conn.setRequestMethod("POST");
  21. conn.setConnectTimeout(5000);
  22. conn.setDoOutput(true); //允許通過POST方式提交數據,必須允許輸出
  23. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  24. conn.setRequestProperty("Content-Length", String.valueOf(data.length));
  25. OutputStream outStream = conn.getOutputStream();
  26. outStream.write(data);
  27. outStream.flush();
  28. outStream.close();
  29. if (conn.getResponseCode() == 200) {
  30. return true;
  31. }
  32. return false;
  33. }

修改save方法

  1. // return sendGetRequestHttpClient(path, params, "UTF-8");
  2. return sendPostRequest(path, params, "UTF-8");
Copyright © Linux教程網 All Rights Reserved