android集合SSH搭建服务器客户端请求
小弟以前是学的J2EE,由于项目需要要开发android,所以临时补了一个多星期,主要是手机端和服务器端交互,双向开发的。
首先在服务器端,我采用的是SSH框架,struts2集合了json插件,服务器和客户端的信息交互采用的JSON来传输,由于在服务器端用了Struts2,所以我就用装了一个JSON插件,这样,很轻易的就把服务器端的信息用JSON的形式发送到了手机端~~以下是代码,欢迎拍砖~~
首先,在服务器端搭建好SSH框架,具体细节就不在陈述~struts xml配置如下:
<package name="login" extends="json-default"> <action name="login" class="com.jclick.test.LoginAction" method="login"> <result type="json"><paramname="includeProperties">result</param></result> </action> </package>
手机端的代码如下:
首先,手机端有一个缓存类,主要用于缓存一些手机端需要访问的数据,这样的好处是可以达达节省手机和服务器的交互,用单例实现的:
package com.jclick.cache; import com.jclick.bean.User; public class Cache { private User User; private Cache(){ } /** 构造单例 */ private static class CacheHolder{ private static final Cache INSTANCE = new Cache(); } public Cache getInstance(){ return CacheHolder.INSTANCE; } public User getUser() { return User; } public void setUser(User User) { this.User = User; } }
接着开始书写手机端的协议,用户向服务器发送请求,同时服务器反馈给手机端信息的:
package com.jclick.protocol; 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.HttpClient; 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 HttpClient httpClient; private HttpPost httpRequest; private HttpResponse response; private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); BaseProtocol() { httpClient = new DefaultHttpClient(); } /** * 向服务器端发送请求 * * @param url * @throws Exception */ protected void pack(String url) throws Exception { httpClient = new DefaultHttpClient(); httpRequest = new HttpPost(url); httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair)); response = httpClient.execute(httpRequest); } /** * 得到返回数据 * * @param url * @return * @throws Exception */ protected void parse() throws Exception { // TODO 状态处理 500 200 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); } } } /** * 向服务器发送信息 * * @param key * @param value */ public void addNameValuePair(String key, String value) { nameValuePair.add(new BasicNameValuePair(key, value)); } /** * 返回JSONObject对象数据模型 * * @return * @throws JSONException */ public JSONObject getJSON() throws JSONException { return new JSONObject(sb.toString()); } }
接着是登陆协议,在这里我只是模拟登陆使用的一个类,仅供大家参考:
package com.jclick.protocol; import org.json.JSONObject; import com.jclick.bean.User; public class LoginProtocol extends BaseProtocol{ private final static String URL = "http://localhost:8080/test/login"; public boolean checkLogin(User usr){ try { pack(URL); parse(); JSONObject obj = this.getJSON(); if(obj.getString("result").equals("failed")){ return false; }else{ return true; } } catch (Exception e) { e.printStackTrace(); return false; } } }
然后是User实体类,主要用于保存用户信息
package com.jclick.bean; public class User { private String username; private String password; 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; } }
最后就是LoginActivity里边判断登陆的代码了,详细代码不再贴出来了,仅贴一个判断登陆的代码:
private void checkedData(){ username = ((EditText)findViewById(R.id.username)).getText().toString(); password = ((EditText)findViewById(R.id.password)).getText().toString(); User user = new User(); user.setUsername(username); user.setPassword(password); LoginProtocol login = new LoginProtocol(); boolean result = login.checkLogin(user); if(result){ SpiderCache.getInstance().setUserSession(user); Toast.makeText(getApplicationContext(), "登录成功", 1000).show(); Intent intent = new Intent (); intent.setClass(LoginActivity.this,WelcomeActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this,"密码或用户名不匹配,请重新输入!",1000).show(); } }
以上代码为了跟大家分享一下,感觉手机端和服务器双向开发非常爽~~同时对android的兴趣大大提升,它也没有我想象中的那么难~~~