全部博文(153)
分类: LINUX
2010-12-17 09:51:23
本篇日志是对Android与WEB应用服务之间进行数据交互的总结,下篇日志是一个经典而又让人十分 好奇的Android多线程断点下载应用的总结。下面我们开始Android与网络数据的交互。
一、创建WEB应用服务
使用eclipse3.5创 建一个动态WEB应用,使用Struts1处理用户请求。我们此应用添加一个DispatchAction,并为它添加四个方法创建用于处理Android以各种方式提交的请求。
1.创建动态WEB工程
Project name:AndroidWebServer
Target runtime:Apache Tomcat v6.0
Dynamic web module version:2.5
Configuration:Default Configuration for Apache Tomcat v6.0
2.添加DispatchAction
package com.changcheng.web.struts.actions;
import java.io.File; import java.io.FileOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.changcheng.web.struts.forms.DataForm;
public class AndroidWebServer extends DispatchAction {
// Andoird以Get方式发送的请求 public ActionForward sendDataByGet(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String name = request.getParameter("name"); request.setAttribute("message", "Hello " + name); return mapping.findForward("success"); }
// Andoird以Post方式发送的请求 public ActionForward sendDataByPost(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String name = request.getParameter("name"); request.setAttribute("message", "Hello " + name); return mapping.findForward("success"); }
// Andoird以表单方式发送的请求 public ActionForward sendDataByForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DataForm formbean = (DataForm) form; System.out.println("StrData:" + formbean.getStrData()); // 获取 上传的文件 if (formbean.getFileData() != null && formbean.getFileData().getFileSize() > 0) { // 设置 保存目录 File dir = new File(request.getSession().getServletContext() .getRealPath("/images")); if (!dir.exists()) dir.mkdirs(); // 保存 文件 FileOutputStream outStream = new FileOutputStream(new File(dir, formbean.getFileData().getFileName())); outStream.write(formbean.getFileData().getFileData());// 保存文件 outStream.close(); } return null; } } |
3.向web.xml添加Struts1的ActionServlet
<servlet> <servlet-name>strutsservlet-name> <servlet-class>org.apache.struts.action.ActionServletservlet-class> <init-param> <param-name>configparam-name> <param-value>/WEB-INF/struts-config.xmlparam-value> init-param> servlet> <servlet-mapping> <servlet-name>strutsservlet-name> <url-pattern>*.dourl-pattern> servlet-mapping> |
4.struts-config.xml
xml version="1.0" encoding="UTF-8"?> DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "">
<struts-config> <form-beans> <form-bean name="dataForm" type="com.changcheng.web.struts.forms.DataForm" /> form-beans> <action-mappings> <action path="/server" type="com.changcheng.web.struts.actions.AndroidWebServer" name="dataForm" scope="request" parameter="method"> <forward name="success" path="/WEB-INF/pages/success.jsp"/> action> action-mappings> struts-config> |
二、创建Android应用
1.创建Android工程
Project name:AndroidWebClient
BuildTarget:Android2.1
Application name:AndroidWEB应用客户端
Package name:com.changcheng.web.client
Create Activity:AndroidWebClient
Min SDK Version:7
2.AndroidManifest.xml
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="" package="com.changcheng.web.client" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" /> <activity android:name=".AndroidWebClient" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity>
application> <uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.changcheng.web.client" android:label="Tests for My App" /> manifest> |
Android应用要访问Internet需 要添加权限。
chinaunix网友2010-12-17 15:03:43
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com