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");