Android应用开发之Android平台向web应用提交信息
来源:互联网 发布:ubuntu 新建 文件 权限 编辑:程序博客网 时间:2024/04/30 01:43
实验:提交视频名称、时长信息
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.1Accept: 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.5User-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-urlencodedAccept-Encoding: gzip, deflateHost: 192.168.1.102:8080Content-Length: 34Connection: Keep-AliveCache-Control: no-cacheCookie: 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");
运行客户端,测试代码,查看服务器后台打印
解释Post方式下中文乱码的处理:服务器端增加过滤器
HttpClient
简介
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.1.1
HttpClient 功能介绍
以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页。
(1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
(2)支持自动转向
(3)支持 HTTPS 协议
(4)支持代理服务器等
实验步骤
在VideoService中添加方法sendRequestHttpClient
// HTTPS SSL COOKIE private static boolean sendRequestHttpClient(String path, Map<String, String> params, String encoding) throws Exception { List<NameValuePair> paramPairs = new ArrayList<NameValuePair>(); if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry: params.entrySet()){ BasicNameValuePair param = new BasicNameValuePair(entry.getKey(),entry.getValue()); paramPairs.add(param); } } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramPairs, encoding); HttpPost post = new HttpPost(path); post.setEntity(entity); DefaultHttpClient client = new DefaultHttpClient(); //浏览器 HttpResponse response = client.execute(post); /* * HTTP/1.1 200 OK * Server: Apache-Coyote/1.1 * Content-Type: text/html;charset=UTF-8 * Content-Length: 14 * Date: Thu, 22 Dec 2011 15:07:38 GMT * *淇濆瓨鎴愬 **/ if(response.getStatusLine().getStatusCode() == 200){ return true; } return false; }
修改save方法
// return sendGetRequestHttpClient(path, params, "UTF-8");// return sendPostRequest(path, params, "UTF-8"); return sendRequestHttpClient(path, params, "UTF-8");
运行客户端,测试,查看服务器输出
- Android应用开发之Android平台向web应用提交信息
- Android平台向web应用提交信息
- Android开发之web应用提交信息
- Android平台向web应用get、post方式提交信息案例
- Android平台向web应用get、post方式提交信息案例
- android平台led开发之应用层
- Android开发入门之通过GET和POST方式提交参数给web应用
- Android Wear之android穿戴式设备应用开发平台
- 【android,7】7.android在web下的应用-将客户端信息提交到服务端
- Android之应用开发
- Android如何查看应用签名信息--微信平台开发应用的签名
- Android Web 应用开发详解
- android平台led开发之应用层(四)
- Android应用开发之TableLayout (表格布局)+信息列表案例
- Android提交应用市场
- Android应用开发之获取web服务器xml数据
- android web应用开发之——HTML5 Storage API
- Android应用开发之获取web服务器xml数据
- android学习笔记9:TableLayout
- web自定义标签摘录
- 经典FormsAuthenticationTicket 分析
- Hadoop&Hbase 备份方案--Pacemaker&DRBD
- C++类静态变量使用注意的问题
- Android应用开发之Android平台向web应用提交信息
- c++之小试牛刀
- Kindle Fire昙花一现?
- 基于struts2+spring+ibatis彩色随机验证码的实现
- hdoj 1335
- 电脑死机原因大全
- Android自动测试之monkeyrunner工具及Monkey工具
- VC 窗口透明
- Windows CE平台定制