android發送/接收json數據

客戶端向服務器端發送數據,這裏用到了兩種,一種是在url中帶參數,一種是json數據發送方式;

url帶參數的寫法:

url+/?r=m/calendar/contact_list&uid=3&&subscriptionslist[pageindex]=10&subscriptionslist[recordlimit]=10

 

 

從“&”符號之後一連串都是參數。

發送方式代碼編寫"

Java代碼  收藏代碼
  1. DefaultHttpClient httpClient = new DefaultHttpClient();  
  2.         HttpPost httpPost = new HttpPost(HttpUtil.BASIC_URL  
  3.                 + HttpUtil.SUBSCRIPTION_URL);  
  4.         try{  
  5.             if (cookie != null) {  
  6.                // httpClient.setCookieStore(LoginJsonUtil.cookie);  
  7.                 List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);  
  8.                 nameValuePair.add(new BasicNameValuePair("uid",  
  9.                         uid));  
  10.                 nameValuePair.add(new BasicNameValuePair("subscriptionslist[pageindex]",  
  11.                         subscriptionslist_pageindex));  
  12.                 nameValuePair.add(new BasicNameValuePair("subscriptionslist[recordlimit]",  
  13.                         subscriptionslist_recordlimit));  
  14.                 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));  
 

 

傳遞參數使用nameValuePair。

如果使用cookie的話,使用上段代碼中註釋掉的部分

httpClient.setCookieStore(LoginJsonUtil.cookie);

 

使用json數據格式發送信息向服務器端:

 

Java代碼  收藏代碼
  1. HttpClient httpClient = new DefaultHttpClient();  
  2.         try {  
  3.             HttpPost httpPost = new HttpPost(BASIC_URL + url);  
  4.             List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
  5.             JSONObject jsonObject = new JSONObject();  
  6.             JSONObject jsonObject2 = new JSONObject();  
  7.             jsonObject.put("uemail", userbean.getEmail());  
  8.             jsonObject.put("password", userbean.getPassword());  
  9.             jsonObject2.put("userbean", jsonObject);  
  10.             nameValuePair.add(new BasicNameValuePair("jsonString", jsonObject  
  11.                     .toString()));  
  12.             Log.i("lifeweeker", jsonObject2.toString());  
  13.             httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));  
 

這裏每個put的順序我不清楚有沒有規定,我是嚴格按照提供的前後順序來組合json數據格式的。

 

 

前面我有用到android發送json數據;這裏我想總結一下我用到的解析json數據格式的方式

json數據格式解析我自己分爲兩種;

一種是普通的,一種是帶有數組形式的;

 

 

普通形式的:

服務器端返回的json數據格式如下:

{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

分析代碼如下:

 

Java代碼  收藏代碼
  1. // TODO 狀態處理 500 200  
  2.                 int res = 0;  
  3.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();  
  4.                 if (res == 200) {  
  5.                      
  6.                     HttpResponse httpResponse = httpClient.execute(httpPost);  
  7.                     StringBuilder builder = new StringBuilder();  
  8.                     BufferedReader bufferedReader2 = new BufferedReader(  
  9.                             new InputStreamReader(httpResponse.getEntity().getContent()));  
  10.                     String str2 = "";  
  11.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2  
  12.                             .readLine()) {  
  13.                         builder.append(s);  
  14.                     }  
  15.                     Log.i("cat"">>>>>>" + builder.toString());  
  16. JSONObject jsonObject = new JSONObject(builder.toString())  
  17.                         .getJSONObject("userbean");  
  18.                 String Uid;  
  19.                 String Showname;  
  20.                 String Avtar;  
  21.                 String State;  
  22.                 Uid = jsonObject.getString("Uid");  
  23.                 Showname = jsonObject.getString("Showname");  
  24.                 Avtar = jsonObject.getString("Avtar");  
  25.                 State = jsonObject.getString("State");  
 

帶數組形式的:

服務器端返回的數據格式爲:

 

Java代碼  收藏代碼
  1. {"calendar":  
  2.     {"calendarlist":  
  3.             [  
  4.            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},  
  5.            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}  
  6.             ]  
  7.     }  
  8. }  
  9. 分析代碼如下:  
  10. // TODO 狀態處理 500 200  
  11.                 int res = 0;  
  12.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();  
  13.                 if (res == 200) {  
  14.                      
  15.                     HttpResponse httpResponse = httpClient.execute(httpPost);  
  16.                     StringBuilder builder = new StringBuilder();  
  17.                     BufferedReader bufferedReader2 = new BufferedReader(  
  18.                             new InputStreamReader(httpResponse.getEntity().getContent()));  
  19.                     String str2 = "";  
  20.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2  
  21.                             .readLine()) {  
  22.                         builder.append(s);  
  23.                     }  
  24.                     Log.i("cat"">>>>>>" + builder.toString());  
  25.                      
  26.                     JSONObject jsonObject = new JSONObject(builder.toString())  
  27.                             .getJSONObject("calendar");  
  28.                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");  
  29.                     for(int i=0;i<jsonArray.length();i++){  
  30.                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);  
  31.                         CalendarInfo calendarInfo = new CalendarInfo();  
  32.                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));  
  33.                         calendarInfo.setTitle(jsonObject2.getString("title"));  
  34.                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));  
  35.                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));  
  36.                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));  
  37.                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));  
  38.                         calendarInfos.add(calendarInfo);  
  39.                     }  
 

總結,普通形式的只需用JSONObject ,帶數組形式的需要使用JSONArray 將其變成一個list。

發佈了6 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章