Android使用Gson解析JSON连接服务器实现客户端登陆功能

服务器端:(需要有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 list = new ArrayList();  
   //  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>() {  
       }.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


struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*

struts.xml


"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

 
 
 
 

 


服务器端:(注意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  getListFromJson(String json){  
     java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken>() {  
     }.getType(); 
     Gson gson = new Gson(); 
     List  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 list = GsonUtil.getListFromJson(result);

String username=null;
String passwd=null;

for (int i=0;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();

你可能感兴趣的:(Android使用Gson解析JSON连接服务器实现客户端登陆功能)