• Android腾讯微博客户端开发二:相关工具篇


    工欲善其事,必先利其器。先给大家介绍3个我在做腾讯微博客户端开发过程中用到的3个工具

    1:一个是sqllite的数据库管理工具,http://www.sqliteexpert.com/download.html 

    2:腾讯API测试工具http://open.t.qq.com/resource.php?i=3,4 

    3:腾讯API返回的都是json和xml的数据格式,在手机上一般用json,这是一个json字符串分析工具。



    把返回的字符串贴在viewer中:



    点击第一个Tab,

    Java代码  收藏代码
    1. json数据格式解析我自己分为两种;  
    2.   
    3. 一种是普通的,一种是带有数组形式的;  
    4.   
    5.   
    6.    
    7.   
    8. 普通形式的:  
    9. 服务器端返回的json数据格式如下:  
    10.   
    11. {"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}  
    12.   
    13. 分析代码如下:  
    14.   
    15. // TODO 状态处理 500 200   
    16.                 int res = 0;   
    17.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
    18.                 if (res == 200) {   
    19.                     /*  
    20.                      * 当返回码为200时,做处理  
    21.                      * 得到服务器端返回json数据,并做处理  
    22.                      * */   
    23.                     HttpResponse httpResponse = httpClient.execute(httpPost);   
    24.                     StringBuilder builder = new StringBuilder();   
    25.                     BufferedReader bufferedReader2 = new BufferedReader(   
    26.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
    27.                     String str2 = "";   
    28.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
    29.                             .readLine()) {   
    30.                         builder.append(s);   
    31.                     }   
    32.                     Log.i("cat"">>>>>>" + builder.toString());  
    33.   
    34. JSONObject jsonObject = new JSONObject(builder.toString())   
    35.                         .getJSONObject("userbean");   
    36.   
    37.                 String Uid;   
    38.                 String Showname;   
    39.                 String Avtar;   
    40.                 String State;   
    41.   
    42.                 Uid = jsonObject.getString("Uid");   
    43.                 Showname = jsonObject.getString("Showname");   
    44.                 Avtar = jsonObject.getString("Avtar");   
    45.                 State = jsonObject.getString("State");  
    46. 带数组形式的:  
    47. 服务器端返回的数据格式为:  
    48.   
    49. {"calendar":   
    50.     {"calendarlist":   
    51.             [   
    52.             {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},   
    53.             {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}   
    54.             ]   
    55.     }   
    56. }  
    57.   
    58. 分析代码如下:  
    59.   
    60. // TODO 状态处理 500 200   
    61.                 int res = 0;   
    62.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
    63.                 if (res == 200) {   
    64.                     /*  
    65.                      * 当返回码为200时,做处理  
    66.                      * 得到服务器端返回json数据,并做处理  
    67.                      * */   
    68.                     HttpResponse httpResponse = httpClient.execute(httpPost);   
    69.                     StringBuilder builder = new StringBuilder();   
    70.                     BufferedReader bufferedReader2 = new BufferedReader(   
    71.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
    72.                     String str2 = "";   
    73.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
    74.                             .readLine()) {   
    75.                         builder.append(s);   
    76.                     }   
    77.                     Log.i("cat"">>>>>>" + builder.toString());   
    78.                     /**  
    79.                      * 这里需要分析服务器回传的json格式数据,  
    80.                      */   
    81.                     JSONObject jsonObject = new JSONObject(builder.toString())   
    82.                             .getJSONObject("calendar");   
    83.                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");   
    84.                     for(int i=0;i<jsonArray.length();i++){   
    85.                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);   
    86.                         CalendarInfo calendarInfo = new CalendarInfo();   
    87.                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));   
    88.                         calendarInfo.setTitle(jsonObject2.getString("title"));   
    89.                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));   
    90.                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));   
    91.                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));   
    92.                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));   
    93.                         calendarInfos.add(calendarInfo);   
    94.                     }  
    95.   
    96. 总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。  
    • JsonViewer.zip (44.2 KB)
    • 描述: json字符串分析工具
    • 下载次数: 336
  • 相关阅读:
    由浅入深——从ArrayList浅谈并发容器
    Lambda表达式
    JVM初体验
    Redis主从复制
    Redis事务
    Redis基本命令
    Redis概述与安装
    Linux之SSH免密登录
    源码安装Nginx以及用systemctl管理
    CentOS 7 :Failed to start IPv4 firewall with iptables.
  • 原文地址:https://www.cnblogs.com/afly/p/2360231.html
Copyright © 2020-2023  润新知