android发送/接收json数据 – xSTARx – ITeye技术网站

客户端向服务器端发送数据,这里用到了两种,一种是在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。

来源URL:http://407827531.iteye.com/blog/1266217