普通形式的:
服务器端返回的json数据格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
分析代码如下:
总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。
服务器端返回的json数据格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
分析代码如下:
01 | // TODO 状态处理 500 200 |
02 | int res = ; |
03 | res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); |
04 | if (res == 200 ) { |
05 | /** |
06 | * 当返回码为200时,做处理 |
07 | * 得到服务器端返回json数据,并做处理 |
08 | * */ |
09 | HttpResponse httpResponse = httpClient.execute(httpPost); |
10 | StringBuilder builder = new StringBuilder(); |
11 | BufferedReader bufferedReader2 = new BufferedReader( |
12 | new InputStreamReader(httpResponse.getEntity().getContent())); |
13 | String str2 = "" ; |
14 | for (String s = bufferedReader2.readLine(); s != null ; s = bufferedReader2 |
15 | .readLine()) { |
16 | builder.append(s); |
17 | } |
18 | Log.i( "cat" , ">>>>>>" + builder.toString()); |
19 |
20 | JSONObject jsonObject = new JSONObject(builder.toString()) |
21 | .getJSONObject( "userbean" ); |
22 |
23 | String Uid; |
24 | String Showname; |
25 | String Avtar; |
26 | String State; |
27 |
28 | Uid = jsonObject.getString( "Uid" ); |
29 | Showname = jsonObject.getString( "Showname" ); |
30 | Avtar = jsonObject.getString( "Avtar" ); |
31 | State = jsonObject.getString( "State" ); |
32 | 带数组形式的: |
33 | 服务器端返回的数据格式为: |
34 |
35 | { "calendar" : |
36 | { "calendarlist" : |
37 | [ |
38 | { "calendar_id" : "1705" , "title" : "(\u4eb2\u5b50)ddssd" , "category_name" : "\u9ed8\u8ba4\u5206\u7c7b" , "showtime" : "1288927800" , "endshowtime" : "1288931400" , "allDay" : false }, |
39 | { "calendar_id" : "1706" , "title" : "(\u65c5\u884c)" , "category_name" : "\u9ed8\u8ba4\u5206\u7c7b" , "showtime" : "1288933200" , "endshowtime" : "1288936800" , "allDay" : false } |
40 | ] |
41 | } |
42 | } |
43 |
44 | 分析代码如下: |
45 |
46 | // TODO 状态处理 500 200 |
47 | int res = ; |
48 | res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); |
49 | if (res == 200 ) { |
50 | /** |
51 | * 当返回码为200时,做处理 |
52 | * 得到服务器端返回json数据,并做处理 |
53 | * */ |
54 | HttpResponse httpResponse = httpClient.execute(httpPost); |
55 | StringBuilder builder = new StringBuilder(); |
56 | BufferedReader bufferedReader2 = new BufferedReader( |
57 | new InputStreamReader(httpResponse.getEntity().getContent())); |
58 | String str2 = "" ; |
59 | for (String s = bufferedReader2.readLine(); s != null ; s = bufferedReader2 |
60 | .readLine()) { |
61 | builder.append(s); |
62 | } |
63 | Log.i( "cat" , ">>>>>>" + builder.toString()); |
64 | /** |
65 | * 这里需要分析服务器回传的json格式数据, |
66 | */ |
67 | JSONObject jsonObject = new JSONObject(builder.toString()) |
68 | .getJSONObject( "calendar" ); |
69 | JSONArray jsonArray = jsonObject.getJSONArray( "calendarlist" ); |
70 | for ( int i=;i<jsonArray.length();i++){ |
71 | JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); |
72 | CalendarInfo calendarInfo = new CalendarInfo(); |
73 | calendarInfo.setCalendar_id(jsonObject2.getString( "calendar_id" )); |
74 | calendarInfo.setTitle(jsonObject2.getString( "title" )); |
75 | calendarInfo.setCategory_name(jsonObject2.getString( "category_name" )); |
76 | calendarInfo.setShowtime(jsonObject2.getString( "showtime" )); |
77 | calendarInfo.setEndtime(jsonObject2.getString( "endshowtime" )); |
78 | calendarInfo.setAllDay(jsonObject2.getBoolean( "allDay" )); |
79 | calendarInfos.add(calendarInfo); |
80 | } |