Android与服务器端数据交互(http协议整合struts2+android)

来源:互联网 发布:java整形转换成字符串 编辑:程序博客网 时间:2024/06/07 10:44

在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有

一:基于Http协议获取数据方法。二:基于SAOP协议获取数据方法,三:忘了-------

那么我们的这篇文章主要是将关于使用Http协议获取服务器端数据,这里我们采取的服务器端技术为java,框架为Struts2,或者可以有Servlet,又或者可直接从JSP页面中获取数据。

那么,接下来我们便开始这一路程:

首先:编写服务器端方法,我这里采用的MVC框架是Struts2,目的很单纯,就是为了以后做个完整的商业项目,技术配备为:android+SSH。当然,篇幅有限,我这里就直接用Strtus2而已。

服务器端:新建WebProject ,选择Java ee 5.0.

为了给项目添加Struts2的支持,我们必须导入Struts2的一些类库,如下即可(有些jar包是不必的,但是我们后来扩展可能是要使用到的,就先弄进去):

1: xwork-core-2.2.1.1.jar

2: struts2-core-2.2.1.1.jar

3: commons-logging-1.0.4.jar

4: freemarker-2.3.16.jar

5: ognl-3.0.jar

6: javassist-3.7.ga.jar

7:commons-ileupload.jar 

8:commons-io.jar

9:json-lib-2.1-jdk15.jar  处理JSON格式数据要使用到

10:struts2-json-plugin-2.2.1.1.jar    基于struts2的json插件

以上的jar包,需要放在WebRoot/WEB-INF/lib目录下

然后在web.xml文件中敲下:

01<?xml version="1.0" encoding="UTF-8"?>
02<web-app version="2.5"
03    xmlns="http://java.sun.com/xml/ns/javaee"
04    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
06    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07     
08     
09    <!-- 定义Struts2的核心控制器:FilterDispatcher -->
10    <filter>
11       <!-- 定义核心Filter的名称 -->
12       <filter-name>struts2</filter-name>
13       <!-- 定义Filter的实现类 -->
14       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
15    </filter>
16     
17    <filter-mapping>
18       <filter-name>struts2</filter-name>
19       <url-pattern>/*</url-pattern>
20    </filter-mapping>
21      
22  <welcome-file-list>
23    <welcome-file>index.jsp</welcome-file>
24  </welcome-file-list>
25   
26</web-app>
然后编写struts.xml文件,并放在WebRoot/WEB-INF/lib目录下:如下代码:
01<?xml version="1.0" encoding="UTF-8"?>
02<!DOCTYPE struts PUBLIC
03    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04    "http://struts.apache.org/dtds/struts-2.0.dtd">
05 <struts>
06     
07    <!-- setting encoding,DynamicMethod,language
08     <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
09    -->
10    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
11    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
12     
13     
14    <!-- add package here extends="struts-default"-->
15     <package name="dongzi" extends="json-default"<!--需要将struts-default改为json-default-->
16      <!-- setting action -->
17         <action name="login" class="com.dongzi.action.loginAction" method="login">
18              <result type="json"></result>  <!--返回值类型设置为json,不设置返回页面-->       
19        </action>
20     </package>
21 </struts>

配置好后,我们再根据<action>标签内容来编写action。方法为method对应的login,类名为loginAction,

注意:包继承为:json-default ,输出结果类型为json

如下:
查看源码
打印?
01public class loginAction extends ActionSupport implements
02                         ServletRequestAware,ServletResponseAware {
03    /**
04     *
05     */
06    private static final long serialVersionUID = 1L;
07      
08    HttpServletRequest request;
09    HttpServletResponse response;
10 
11    public void setServletRequest(HttpServletRequest request) {
12     this.request=request;
13    }
14 
15    public void setServletResponse(HttpServletResponse response) {
16        this.response=response;
17    }
18 
19    public void  login(){ 
20        try {
21             //HttpServletRequest request =ServletActionContext.getRequest();
22             // HttpServletResponse response=ServletActionContext.getResponse();
23             this.response.setContentType("text/html;charset=utf-8");
24             this.response.setCharacterEncoding("UTF-8");
25                 if(this.request.getParameter("username").equals("123456")){
26                      this.response.getWriter().write("真的很奇怪,日本人!");
27                 }else if(this.request.getParameter("username").equals("zhd")){
28                     this.response.getWriter().write("没有错,我就是东子哥!");
29                 }else{
30                     this.response.getWriter().write("我就是东子哥!");
31                 }
32                  
33            //将要返回的实体对象进行json处理     
34              //  JSONObject json=JSONObject.fromObject(this.getUsername());    
35             //输出格式如:{"id":1, "username":"zhangsan", "pwd":"123"}     
36             //   System.out.println(json);      
37             
38              //   this.response.getWriter().write(json.toString());
39            /**
40               JSONObject json=new JSONObject();
41               json.put("login", "login");
42                response.setContentType("text/html;charset=utf-8");
43               System.out.println(json);
44               byte[] jsonBytes = json.toString().getBytes("utf-8");
45               response.setContentLength(jsonBytes.length);
46               response.getOutputStream().write(jsonBytes);
47               **/
48            /**
49              JSONObject json=new JSONObject();
50               json.put("login", "login");
51               byte[] jsonBytes = json.toString().getBytes("utf-8");
52               response.setContentType("text/html;charset=utf-8");
53               response.setContentLength(jsonBytes.length);
54               response.getOutputStream().write(jsonBytes);
55               response.getOutputStream().flush();
56               response.getOutputStream().close();   
57             **/ 
58              
59        catch (Exception e) {
60            e.printStackTrace();
61        }
62        // return null;
63    }
64}转:http://www.open-open.com/lib/view/open1326871162327.html
原创粉丝点击