Android用GSon处理Json数据


Android用GSon处理Json数据:

  1. package com.demo;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.params.BasicHttpParams;  
  12. import org.apache.http.protocol.HTTP;  
  13.   
  14. import android.util.Log;  
  15.   
  16. public class WebDataGetApi {  
  17.   
  18.     private static final String TAG = "WebDataGetAPI";  
  19.     private static final String USER_AGENT = "Mozilla/4.5";  
  20.   
  21.     protected String getRequest(String url) throws Exception {  
  22.         return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));  
  23.     }  
  24.   
  25.     protected String getRequest(String url, DefaultHttpClient client)  
  26.             throws Exception {  
  27.         String result = null;  
  28.         int statusCode = 0;  
  29.         HttpGet getMethod = new HttpGet(url);  
  30.         Log.d(TAG, "do the getRequest,url=" + url + "");  
  31.         try {  
  32.             getMethod.setHeader("User-Agent", USER_AGENT);  
  33.             // HttpParams params = new HttpParams();   
  34.   
  35.             // 添加用户密码验证信息   
  36.             // client.getCredentialsProvider().setCredentials(   
  37.             // new AuthScope(null, -1),   
  38.             // new UsernamePasswordCredentials(mUsername, mPassword));   
  39.   
  40.             HttpResponse httpResponse = client.execute(getMethod);  
  41.             // statusCode == 200 正常   
  42.             statusCode = httpResponse.getStatusLine().getStatusCode();  
  43.             Log.d(TAG, "statuscode = " + statusCode);  
  44.             // 处理返回的httpResponse信息   
  45.             result = retrieveInputStream(httpResponse.getEntity());  
  46.         } catch (Exception e) {  
  47.             Log.e(TAG, e.getMessage());  
  48.             throw new Exception(e);  
  49.         } finally {  
  50.             getMethod.abort();  
  51.         }  
  52.         return result;  
  53.     }  
  54.   
  55.     /** 
  56.      * 处理httpResponse信息,返回String 
  57.      *  
  58.      * @param httpEntity 
  59.      * @return String 
  60.      */  
  61.     protected String retrieveInputStream(HttpEntity httpEntity) {  
  62.         int length = (int) httpEntity.getContentLength();  
  63.         if (length < 0)  
  64.             length = 10000;  
  65.         StringBuffer stringBuffer = new StringBuffer(length);  
  66.         try {  
  67.             InputStreamReader inputStreamReader = new InputStreamReader(  
  68.                     httpEntity.getContent(), HTTP.UTF_8);  
  69.             char buffer[] = new char[length];  
  70.             int count;  
  71.             while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {  
  72.                 stringBuffer.append(buffer, 0, count);  
  73.             }  
  74.         } catch (UnsupportedEncodingException e) {  
  75.             Log.e(TAG, e.getMessage());  
  76.         } catch (IllegalStateException e) {  
  77.             Log.e(TAG, e.getMessage());  
  78.         } catch (IOException e) {  
  79.             Log.e(TAG, e.getMessage());  
  80.         }  
  81.         return stringBuffer.toString();  
  82.     }  
  83. }  

二. 建立JsonDataGetApi.java 

  1. package com.demo;  
  2.   
  3. import org.json.JSONArray;  
  4. import org.json.JSONException;  
  5. import org.json.JSONObject;  
  6.   
  7. public class JsonDataGetApi extends WebDataGetApi {  
  8.     private static final String BASE_URL = "http://10.0.2.2:82/AccountService/";  
  9.     private static final String EXTENSION = "Json/";;  
  10.   
  11.     public JSONObject getObject(String sbj) throws JSONException, Exception {  
  12.         return new JSONObject(getRequest(BASE_URL + EXTENSION + sbj));  
  13.     }  
  14.   
  15.     public JSONArray getArray(String sbj) throws JSONException, Exception {  
  16.         return new JSONArray(getRequest(BASE_URL + EXTENSION + sbj));  
  17.     }  
  18. }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页

相关内容