首先在服務器端,我採用的是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的興趣大大提升,它也沒有我想象中的那麼難~~~