服务器端:(需要有gson.jar)
loginPro.action文件
public class LoginAction extends ActionSupport implements ServletRequestAware
,ServletResponseAware{
private static final long serialVersionUID = -3604892179657815531L;
private String username;
private String password;
private String tip;
private HttpServletResponse response;
private HttpServletRequest request;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response= response;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request= request;
}
public void json(){
List<User> list = new ArrayList<User>();
// JsonArray jsonArray = new JsonArray();
// JsonObject jsonObject = new JsonObject();
Gson gson = new Gson();
User u1 = new User();
u1.setId(1);
u1.setNickname("aaa");
u1.setPassword("sss");
u1.setSex("a");
u1.setUsername("ww");
u1.setDiscription("111");
list.add(u1);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<User>>() {
}.getType();
String beanListToJson = gson.toJson(list,type);
System.out.println("GSON-->"+beanListToJson);
try {
response.setCharacterEncoding("GBK");
//response.setContentType("text/xml;charset=utf-8");
this.response.getWriter().write(beanListToJson);
} catch (IOException e) {
e.printStackTrace();
}
}
}
User(客户端服务器端同时需要)
public class User {
// Fields
private Integer id;
private String username;
private String password;
private String nickname;
private String sex;
private String discription;
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(String username, String nickname) {
this.username = username;
this.nickname = nickname;
}
/** full constructor */
public User(String username, String password, String nickname, String sex,
String discription) {
this.username = username;
this.password = password;
this.nickname = nickname;
this.sex = sex;
this.discription = discription;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return this.nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDiscription() {
return this.discription;
}
public void setDiscription(String discription) {
this.discription = discription;
}
}
Struts2在客户端web.xml和struts2.xml中的配置如下
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="person" extends="json-default" namespace="/">
<action name="loginPro" class="com.edu2act.campuslife.struts2.action.LoginAction" method="json">
<result type="json"></result>
</action>
</package>
</struts>
服务器端:(注意Android的版本问题,2.3以前在主线程中调用不需添加子线程)
以下客户端例子是Android4.2版本(添加gson.jar)
public class GsonUtil {
public static String getJson(String url){
HttpClient client = new DefaultHttpClient();
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功
HttpEntity entity = response.getEntity();
if(entity!=null){
String beanListToJson = EntityUtils.toString(entity,"GBK");
return beanListToJson;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
public static List<User> getListFromJson(String json){
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<User>>() {
}.getType();
Gson gson = new Gson();
List<User> list = gson.fromJson(json, type);
return list;
}
}
Activity中调用
user = name.getText().toString();
pass = password.getText().toString();
Thread thread = new Thread(new Runnable() {
public void run() {
String url = "http://10.7.85.10:8080/s2sh0909/loginPro.action";
result = GsonUtil.getJson(url);
List<User> list = GsonUtil.getListFromJson(result);
String username=null;
String passwd=null;
for (int i=0;i<list.size();i++) {
username=list.get(i).getUsername();
passwd=list.get(i).getPassword();
if (username.trim().equals(user.trim())&& passwd.trim().equals(pass.trim())) {
if (chpass.isChecked()) { // 记住用户名、密码、
Editor editor =sp.edit();
editor.putString("USERNAME", user);
editor.putString("PASSWORD", pass);
editor.commit();
} // 跳转界面
Intent i1 = new Intent(getActivity(),MenuActivity.class);
i1.putExtra("login", 1);
startActivity(i1);
}
}
}
});
thread.start();