android 服务器json

简介: 引用:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=69596 首先在服务器端,星空采用的是SSH框架,struts2集合了json插件,服务器和客户端的信息交互采用的JSON来传输,由于在服务器端用了Struts2,所以 星空 就用...

引用:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=69596

首先在服务器端,星空采用的是SSH框架,struts2集合了json插件,服务器和客户端的信息交互采用的JSON来传输,由于在服务器端用了Struts2,所以 星空 就用装了一个JSON插件,这样,很轻易的就把服务器端的信息用JSON的形式发送到了手机端~~以下是代码,欢迎eoe的朋友们拍砖~~

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

Java代码:

  1. <package name="login" extends="json-default">  
  2.      <action name="login" class="com.jclick.test.LoginAction" method="login">  
  3.         <result type="json"><paramname="includeProperties">result</param></result>  
  4.      </action>  
  5. </package>  
复制代码

手机端的代码如下:

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

Java代码:

  1. package com.jclick.cache;  
  2.    
  3. import com.jclick.bean.User;  
  4.   
  5. public class Cache {  
  6.        
  7.      private User User;  
  8.        
  9.      private Cache(){  
  10.            
  11.      }  
  12.      /** 构造单例 */  
  13.      private static class CacheHolder{  
  14.          private static final Cache INSTANCE = new Cache();  
  15.      }  
  16.      public Cache getInstance(){  
  17.          return CacheHolder.INSTANCE;  
  18.      }  
  19.      public User getUser() {  
  20.          return User;  
  21.      }  
  22.      public void setUser(User User) {  
  23.          this.User = User;  
  24.      }  
  25.    
  26. }  
复制代码

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

Java代码:

  1. package com.jclick.protocol;  
  2.    
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.    
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.HttpClient;  
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.apache.http.message.BasicNameValuePair;  
  15. import org.json.JSONException;  
  16. import org.json.JSONObject;  
  17.    
  18. public class BaseProtocol {  
  19.      private StringBuilder sb = new StringBuilder();  
  20.    
  21.      private HttpClient httpClient;  
  22.      private HttpPost httpRequest;  
  23.      private HttpResponse response;  
  24.    
  25.      private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
  26.    
  27.      BaseProtocol() {  
  28.          httpClient = new DefaultHttpClient();  
  29.      }  
  30.    
  31.      /** 
  32.       * 向服务器端发送请求 
  33.       *  
  34.       * @param url 
  35.       * @throws Exception 
  36.       */  
  37.      protected void pack(String url) throws Exception {  
  38.          httpClient = new DefaultHttpClient();  
  39.          httpRequest = new HttpPost(url);  
  40.    
  41.          httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));  
  42.          response = httpClient.execute(httpRequest);  
  43.      }  
  44.    
  45.      /** 
  46.       * 得到返回数据 
  47.       *  
  48.       * @param url 
  49.       * @return 
  50.       * @throws Exception 
  51.       */  
  52.      protected void parse() throws Exception {  
  53.          // TODO 状态处理 500 200  
  54.          if (response.getStatusLine().getStatusCode() == 200) {  
  55.    
  56.              BufferedReader bufferedReader2 = new BufferedReader(  
  57.                      new InputStreamReader(response.getEntity().getContent()));  
  58.              for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2  
  59.                      .readLine()) {  
  60.                  sb.append(s);  
  61.              }  
  62.          }  
  63.      }  
  64.    
  65.      /** 
  66.       * 向服务器发送信息 
  67.       *  
  68.       * @param key 
  69.       * @param value 
  70.       */  
  71.      public void addNameValuePair(String key, String value) {  
  72.          nameValuePair.add(new BasicNameValuePair(key, value));  
  73.      }  
  74.    
  75.      /** 
  76.       * 返回JSONObject对象数据模型 
  77.       *  
  78.       * @return 
  79.       * @throws JSONException 
  80.       */  
  81.      public JSONObject getJSON() throws JSONException {  
  82.          return new JSONObject(sb.toString());  
  83.      }  
  84.    
  85. }  
复制代码

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

Java代码:

  1. package com.jclick.protocol;  
  2.    
  3. import org.json.JSONObject;  
  4.    
  5. import com.jclick.bean.User;  
  6.    
  7. public class LoginProtocol extends BaseProtocol{  
  8.        
  9.      private final static String URL = "http://localhost:8080/test/login";  
  10.        
  11.      public boolean checkLogin(User usr){  
  12.          try {  
  13.              pack(URL);  
  14.              parse();  
  15.              JSONObject obj = this.getJSON();  
  16.              if(obj.getString("result").equals("failed")){  
  17.                  return false;  
  18.              }else{  
  19.                  return true;  
  20.              }  
  21.          } catch (Exception e) {  
  22.              e.printStackTrace();  
  23.              return false;  
  24.          }  
  25.      }  
  26.    
  27. }  
复制代码

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

Java代码:

  1. package com.jclick.bean;  
  2.    
  3. public class User {  
  4.      private String username;  
  5.      private String password;  
  6.      public String getUsername() {  
  7.          return username;  
  8.      }  
  9.      public void setUsername(String username) {  
  10.          this.username = username;  
  11.      }  
  12.      public String getPassword() {  
  13.          return password;  
  14.      }  
  15.      public void setPassword(String password) {  
  16.          this.password = password;  
  17.      }  
  18.    
  19. }  
复制代码

最后就是LoginActivity里边判断登陆的代码了,仅贴一个判断登陆的代码:

Java代码:

  1. private void checkedData(){  
  2.      username = ((EditText)findViewById(R.id.username)).getText().toString();  
  3.      password = ((EditText)findViewById(R.id.password)).getText().toString();  
  4.         
  5.      User user = new User();  
  6.      user.setUsername(username);  
  7.      user.setPassword(password);  
  8.      LoginProtocol login = new LoginProtocol();  
  9.      boolean result = login.checkLogin(user);  
  10.        
  11.      if(result){                 SpiderCache.getInstance().setUserSession(user);  
  12.          Toast.makeText(getApplicationContext(), "登录成功", 1000).show();  
  13.         Intent intent = new Intent ();  
  14.          intent.setClass(LoginActivity.this,WelcomeActivity.class);  
  15.          startActivity(intent);  
  16.      }else{              Toast.makeText(LoginActivity.this,"密码或用户名不匹配,请重新输入!",1000).show();  
  17.      }  
  18. }  
复制代码
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
1月前
|
Ubuntu 网络协议 Java
在Android平板上使用code-server公网远程Ubuntu服务器编程
在Android平板上使用code-server公网远程Ubuntu服务器编程
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
56 0
|
3月前
|
数据采集 编解码 图形学
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
101 0
|
3月前
|
安全 网络协议 Linux
【公网远程手机Android服务器】安卓Termux搭建Web服务器
【公网远程手机Android服务器】安卓Termux搭建Web服务器
63 0
|
4月前
|
XML JSON Android开发
[Android]使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换
[Android]使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换
54 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
58 0
|
11月前
|
JSON Android开发 数据格式
Android:解析Json异常 Expected a string but was BEGIN_OBJECT at
今天解析后端数据时,发现了这个报错:Expected BEGIN_OBJECT but was STRING at 看来是自己哪儿解析错误了。 因为数据的特殊性,后端返回的Json串里面可能还会有Json数据,可能嵌套了三次层,
|
XML 存储 JSON
Android网络与数据存储——网络编程数据处理(网络请求解析Json,解析xml)
Android网络与数据存储——网络编程数据处理(网络请求解析Json,解析xml)
228 0