当前位置: 代码迷 >> Android >> Android(5)数据存储之五网络数据交互 1
  详细解决方案

Android(5)数据存储之五网络数据交互 1

热度:109   发布时间:2016-05-01 14:32:52.0
Android(五)数据存储之五网络数据交互 1

昨天我们只对Android接收网络数据进行了简单介绍,今天我们完成了Android数据存储网络部分的所有内容。在此我将对这非常重要的内容进行总结。

?

?????? 本篇日志是对AndroidWEB应用服务之间进行数据交互的总结,下篇日志是一个经典而又让人十分好奇的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 {

?

???????? // AndoirdGet方式发送的请求

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

???????? }

?

???????? // AndoirdPost方式发送的请求

???????? 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添加Struts1ActionServlet

<servlet>

???????? <servlet-name>struts</servlet-name>

???????? <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

???????? <init-param>

?????????????????? <param-name>config</param-name>

?????????????????? <param-value>/WEB-INF/struts-config.xml</param-value>

???????? </init-param>

</servlet>

<servlet-mapping>

???????? <servlet-name>struts</servlet-name>

???????? <url-pattern>*.do</url-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"

????????? "http://struts.apache.org/dtds/struts-config_1_3.dtd">

?

<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="http://schemas.android.com/apk/res/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" />

?????? <!-- 访问internet权限 -->

?????? <uses-permission android:name="android.permission.INTERNET" />

?????? <!-- SDCard中创建与删除文件权限 -->

?????? <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

?????? <!-- SDCard写入数据权限 -->

?????? <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需要添加权限。

  相关解决方案