android怎么获取服务器数据

2025-04-07 11:39:51
推荐回答(1个)
回答1:

  一:基于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文件中敲下:
  View Code
  
    xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  
  
  
  struts2
  
  org.apache.struts2.dispatcher.FilterDispatcher
  


  
  struts2
  /*
  


  
  index.jsp
  


  

  然后编写struts.xml文件,并放在WebRoot/WEB-INF/lib目录下:如下代码:
  View Code
  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  

  
  
  

  
  
  
  
  
  

  

  

  配置好后,我们再根据标签内容来编写action。方法为method对应的login,类名为loginAction,
  注意:包继承为:json-default ,输出结果类型为json
  如下:
  View Code
  public class loginAction extends ActionSupport implements
  ServletRequestAware,ServletResponseAware {
  /**
  *
  */
  private static final long serialVersionUID = 1L;

  HttpServletRequest request;
  HttpServletResponse response;

  public void setServletRequest(HttpServletRequest request) {
  this.request=request;
  }

  public void setServletResponse(HttpServletResponse response) {
  this.response=response;
  }

  public void login(){
  try {
  //HttpServletRequest request =ServletActionContext.getRequest();
  // HttpServletResponse response=ServletActionContext.getResponse();
  this.response.setContentType("text/html;charset=utf-8");
  this.response.setCharacterEncoding("UTF-8");
  if(this.request.getParameter("username").equals("123456")){
  this.response.getWriter().write("真的很奇怪,日本人!");
  }else if(this.request.getParameter("username").equals("zhd")){
  this.response.getWriter().write("没有错,我就是东子哥!");
  }else{
  this.response.getWriter().write("我就是东子哥!");
  }

  //将要返回的实体对象进行json处理
  // JSONObject json=JSONObject.fromObject(this.getUsername());
  //输出格式如:{"id":1, "username":"zhangsan", "pwd":"123"}
  // System.out.println(json);

  // this.response.getWriter().write(json.toString());
  /**
  JSONObject json=new JSONObject();
  json.put("login", "login");
  response.setContentType("text/html;charset=utf-8");
  System.out.println(json);
  byte[] jsonBytes = json.toString().getBytes("utf-8");
  response.setContentLength(jsonBytes.length);
  response.getOutputStream().write(jsonBytes);
  **/
  /**
  JSONObject json=new JSONObject();
  json.put("login", "login");
  byte[] jsonBytes = json.toString().getBytes("utf-8");
  response.setContentType("text/html;charset=utf-8");
  response.setContentLength(jsonBytes.length);
  response.getOutputStream().write(jsonBytes);
  response.getOutputStream().flush();
  response.getOutputStream().close();
  **/

  } catch (Exception e) {
  e.printStackTrace();
  }
  // return null;
  }
  }