作为一个客户端开发人员,每次让服务端人员写数据接口的的时候,感觉就像是在求他们一样,与其求别人,还不如自己会搞,android与服务端通行,最常见的就是json了,以前用webservice,感觉还可以,总之,多一种通行方式多一套方法,多一个机会吧!大三了,感觉心里惶惶的,但是还是得学习呀。
先看看效果吧
注意:*浏览器的参数名称必须要和action里面的接收的名称一样,要不然接收不到参数*
下面看看一个demo吧!
LoginAction.java
package com.twj.action;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.twj.base.BaseAction;
import com.twj.entity.User;
import com.twj.service.UserService;
@Scope("prototype")
@Component
@Action(value="LoginAction",results={
@Result(name="login",location="/index.jsp")
})
public class LoginAction extends BaseAction implements ServletRequestAware,ServletResponseAware{
private static final long serialVersionUID = 1L;
@Autowired
private UserService userService;
HttpServletRequest request;
HttpServletResponse response;
private String userName;
private String password;
private Integer UId;
private User u;
/** * 模拟用户登录的业务 */
public void login() {
System.out.println("-------------"+userName+password);
try {
//如果不采用接口注入的方式的获取HttpServletRequest,HttpServletResponse的方式
// HttpServletRequest request =ServletActionContext.getRequest();
// HttpServletResponse response=ServletActionContext.getResponse();
//setHandler();
this.response.setContentType("text/json;charset=utf-8");
this.response.setCharacterEncoding("UTF-8");
//JSONObject json=new JSONObject();
Map<String,String> json=new HashMap<String,String>();
u=userService.getUserByAccess(userName, password);
if (u!=null) {
json.put("ULive", u.getULive());
json.put("UWork", u.getUWork());
json.put("USex", u.getUSex());
json.put("UName", u.getUName());
json.put("UBkurl",u.getUBkurl());
json.put("UIphone",u.getUIphone());
}else {
json.put("message", "账号或密码错误");
}
//
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getUserByUId(){
try {//setHandler();
this.response.setContentType("text/json;charset=utf-8");
this.response.setCharacterEncoding("UTF-8");
Map<String, Object>json=new HashMap<String, Object>();
u=userService.getUserById(UId);
if (u!=null) {
json.put("user", u);
}
else {
json.put("enorr", "返回错误");
}
byte[] jsonBytes;
jsonBytes = json.toString().getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
void setHandler(){
};
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public Integer getUId() {
return UId;
}
public void setUId(Integer uId) {
UId = uId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
struts.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" ></constant>
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" />
<!-- <package name="default" namespace="/" extends="struts-default">-->
<package name="default" extends="json-default"><!--需要将struts-default改为-->
<action name="login" class="com.twj.action.LoginAction" method="login">
<result type="json"/>
<!-- <span style="color: #ff0000;"> </span> -->
<!--返回值类型设置为json,不设置返回页面-->
</action>
<action name="getUserByUId" class="com.twj.action.LoginAction" method="getUserByUId">
<result type="json"/>
<!-- <span style="color: #ff0000;"> </span> -->
<!--返回值类型设置为json,不设置返回页面-->
</action>
</package>
<!-- 拦截器配置 -->
</struts>
通过配置及可以实现json发布了,剩下的就是客户端解析json了