strut2服务器与android交互数据,用Android搭建客户端 手机和服务器交互开发

首先在服务器端,采用的是SSH框架,struts

2集合了JSON插件,服务器和客户端的信息交互采用的JSON来传输,由于在服务器端用了Struts

2,所以我就用装了一个JSON插件。这样,很轻易的就把服务器端的信息用JSON的形式发送到了手机端。以下是代码:

首先,在服务器端搭建好SSH框架,具体细节就不在陈述。struts.xml配置如下:

result

result

手机端的代码如下:

首先,手机端有一个缓存类,主要用于缓存一些手机端需要访问的数据,这样的好处是可以达达节省手机和服务器的交互,用单例实现的:

package com.lyc; public class Cache {

private User User; public Cache(){ } private static class CacheHolder{

private static final Cache

INSTANCE=new Cache();

}

public static Cache getInstance(){

return

CacheHolder.INSTANCE;

}

public User getUser(){

return User;

} public void setUser(User User){

this.User=User;

}

}

接着开始书写手机端的协议,用户向服务器发送请求,同时服务器反馈给手机端信息的:

package com.lyc;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.json.JSONException;

import org.json.JSONObject;

public class BaseProtocol {

private StringBuilder sb=new

StringBuilder(); private

org.apache.http.client.HttpClient httpClient;

private HttpPost httpRequest;

private HttpResponse

response; private

List nameValuePair =

new ArrayList();

BaseProtocol(){ httpClient =

new DefaultHttpClient();

} protected void pack(String url) throws

Exception{

httpClient =new

DefaultHttpClient();

httpRequest = new

HttpPost(url); httpRequest.setEntity(new

UrlEncodedFormEntity(nameValuePair));

response=httpClient.execute(httpRequest);

} protected void parse() throws Exception{

//TODO状态处理500200 if(response.getStatusLine().getStatusCode()==200){ BufferedReader

bufferedReader2=new BufferedReader( new

InputStreamReader(response.getEntity().getContent()));

for(String

s=bufferedReader2.readLine(); s!=null;

s=bufferedReader2 .readLine()){

sb.append(s);

} } } public void addNameValuePair(String key,String

value){ nameValuePair.add(new

BasicNameValuePair(key,value));

} public JSONObject getJSON() throws

JSONException{ return new

JSONObject(sb.toString());

} }

接着是登录协议,在这里我只是模拟登陆使用的一个类,仅供大家参考:

importorg.json.JSONObject;

importcom.jclick.bean.User;

publicclassLoginProtocolextendsBaseProtocol{

privatefinalstaticStringURL="http://localhost:8080/test/login";

publicbooleancheckLogin(Userusr){

try{

pack(URL);

parse();

JSONObjectobj=this.getJSON();

if(obj.getString("result").equals("failed")){

returnfalse;

}else{

returntrue;

}

}catch(Exceptione){

e.printStackTrace();

returnfalse;

}

}

}

然后是User实体类,主要用于保存用户信息:

publicclassUser{

privateStringusername;

privateStringpassword;

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

}

最后就是LoginActivity里边判断登陆的代码了,详细代码不再贴出来了,仅贴一个判断登陆的代码:

privatevoidcheckedData(){

username=((EditText)findViewById(R.id.username)).getText().toString();

password=((EditText)findViewById(R.id.password)).getText().toString();

Useruser=newUser();

user.setUsername(username);

user.setPassword(password);

LoginProtocollogin=newLoginProtocol();

booleanresult=login.checkLogin(user);

if(result){SpiderCache.getInstance().setUserSession(user);

Toast.makeText(getApplicationContext(),"登录成功",1000).show();

Intentintent=newIntent();

intent.setClass(LoginActivity.this,WelcomeActivity.class);

startActivity(intent);

}else{Toast.makeText(LoginActivity.this,"密码或用户名不匹配,请重新输入!",1000).show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值