Android Volley获取json格式的数据

  为了让Android能够快速地访问网络和解析通用的数据格式Google专门推出了Volley库,用于Android系统的网络传输。volley库可以方便地获取远程服务器的图片、字符串、json对象和json对象数组等。当然,java本身也有获取json对象的方法,然而为了更好地适应移动互联网,google专门为其做了特殊的优化,因而应该尽可能地使用Volley库。

  Volley官方文档:https://developer.android.com/training/volley/index.html

  Volley库下载:git clone https://android.googlesource.com/platform/frameworks/volley/(由于用到google,因而需要做一些特殊的处理;您可参考前面的文章“git无法clone远程代码库及git代理设置”)

  环境:Android Studio

  一、将Volley库导入到Android工程中(此处采用的是源码导入的方式,更多方式导入方式可参考“Android Studio 简介及导入 jar 包和第三方开源库方法”):

  1、将下载好的源码拷贝到app同级目录下,在setting.gradle下添加volley库支持

include ':app', ':volley'

  2、在app/build.gradle中的dependencies下添加

compile project(':volley')

  最后结果如下:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile project(':volley')
}

  至此,volley裤已经添加到了工程中。下面看看如何通过volley库正确地读取远程服务器上的json对象和json数组。

  由于要访问网络,因此需要对网络权限进行设置。在AndroidManifest.xml中添加如下权限设置。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

  二、获取服务器json对象

  1、服务器中json对象数据格式

{"linkmd5id":"c07e1ddae86029dbd1f4fb79a6f532fc","view":"149","comment":"1","diggnum":"0","postdate":"2015-10-15 12:59","updated":"2015-10-15 07:25:58"}

  2、获取json对象

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String jsonUrl = "http://152.123.55.102:8080/json.html";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                jsonUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                System.out.println("json data===" + response);
            }
        }, new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("发生了一个错误!");
                error.printStackTrace();
            }
        });
        requestQueue.add(jsonObjectRequest);

  3、运行结果:

10-19 16:34:26.376  24583-24583/com.example.news.andoridnewsapp I/System.out﹕ json data==={"diggnum":"0","postdate":"2015-10-15 12:59","view":"149","comment":"1","updated":"2015-10-15 07:25:58","linkmd5id":"c07e1ddae86029dbd1f4fb79a6f532fc"}

  三、获取json对象数组

  1、原数组

[{"linkmd5id":"c07e1ddae86029dbd1f4fb79a6f532fc","title":"u3010u539fu521bu3011ZeroClipboardu7684u65f6u4ee3u6216u8bb8u5df2u7ecfu8fc7u53bbu4e86","desc":"
","link":"http://www.cnblogs.com/mawuhen/p/4882104.html","listurl":null,"view":"149","comment":"1","diggnum":"0","postdate":"2015-10-15 12:59","updated":"2015-10-15 07:25:58"},{"linkmd5id":"3bf8dd1f81a47a83895e8c55669c6631","title":"Web APiu4e4bu6355u83b7u8bf7u6c42u539fu59cbu5185u5bb9u7684u5b9eu73b0u65b9u6cd5u4ee5u53cau63a5u53d7POSTu8bf7u6c42u591au4e2au53c2u6570u591au79cdu89e3u51b3u65b9u6848uff08u5341u56dbuff09","desc":"
","link":"http://www.cnblogs.com/CreateMyself/p/4874273.html","listurl":null,"view":"165","comment":"5","diggnum":"6","postdate":"2015-10-15 12:38","updated":"2015-10-15 07:25:58"},{"linkmd5id":"39375a02b70c6b0a395f42bd97c60cdb","title":"SharePoint APIu6d4bu8bd5u7cfbu5217u2014u2014Records.BypassLocksu6d4bu8bd5","desc":"
","link":"http://www.cnblogs.com/LanTianYou/p/4882050.html","listurl":null,"view":"50","comment":"0","diggnum":"0","postdate":"2015-10-15 12:31","updated":"2015-10-15 07:25:58"},{"linkmd5id":"be0e6182fb6fb216e58d21f08e54278e","title":"C#u59d4u6258u4f7fu7528u8be6u89e3uff08Delegatesuff09","desc":"
","link":"http://www.cnblogs.com/liuhaorain/p/3911845.html","listurl":null,"view":"226","comment":"1","diggnum":"0","postdate":"2015-10-15 11:47","updated":"2015-10-15 07:25:58"},{"linkmd5id":"d85434e0d6e088a972f53447bf769256","title":"golang reflect","desc":"
","link":"http://www.cnblogs.com/coder2012/p/4881854.html","listurl":null,"view":"86","comment":"0","diggnum":"0","postdate":"2015-10-15 11:25","updated":"2015-10-15 07:25:58"},{"linkmd5id":"9e6d443d4c2f489179c22830437e8034","title":"Android Bitmapu5b9eu6218u6280u5de7","desc":"
 u6ce8uff1au672cu6587u5927u91cfu53c2u8003u8c37u6b4cu5b98u65b9u6587u6863u81eahttp://developer.android.com/intl/zh-cn/training/displaying-bitmaps/index.htmlu3002u5982u679cu4f60u81eau5b66u80fdu529bu8fd8u53efu4ee5u6216u8005u82f1u6587u7406u89e3u80fdu529bu4e0du9519u53efu4ee5u76f4u63a5u53bbu770bu539fu7248u7684u3002u5982u679cu4f60u65f6u95f4u5b9du8d35uff0cu60f3u76f4u63a5u770bu7ed3u8bbau548cu6211u4e2au4ebau7406u89e3u7684u5fc3u5f97uff0cu4e5fu53ef... 
 ","link":"http://www.cnblogs.com/punkisnotdead/p/4881771.html","listurl":null,"view":"116","comment":"1","diggnum":"0","postdate":"2015-10-15 11:08","updated":"2015-10-15 07:25:58"},{"linkmd5id":"71bf86ad9eb586aecdca37b9df43acb0","title":"u3010u7ec4u4ef6u5316u5f00u53d1u3011u524du7aefu8fdbu9636u7bc7u4e4bu5982u4f55u7f16u5199u53efu7ef4u62a4u53efu5347u7ea7u7684u4ee3u7801","desc":"
","link":"http://www.cnblogs.com/yexiaochai/p/4876099.html","listurl":null,"view":"510","comment":"7","diggnum":"19","postdate":"2015-10-15 11:05","updated":"2015-10-15 07:25:58"},{"linkmd5id":"794568d4689bf1c3fc5b6de7945cd978","title":"WPFu5165u95e8u6559u7a0bu7cfbu5217u4e8cu5341u4e00u2014u2014DataGridu793au4f8b(u4e00)","desc":"
 u524du9762u6211u4eecu5b66u4e60u4e86ListViewu63a7u4ef6u7684u4f7fu7528u793au4f8buff0cu4ecau5929u6211u4eecu6765u5b66u4e60DataGridu7684u6709u5173u77e5u8bc6u3002u63d0u5230DataGrid u4e0du7ba1u662fAsp.Netu4e2du7684u7f51u9875u5f00u53d1u8fd8u662fWinFormu5e94u7528u7a0bu5e8fu5f00u53d1u90fdu4f1au9891u7e41u4f7fu7528u3002u901au8fc7u5b83u6211u4eecu53efu4ee5u7075u6d3bu7684u5728u884cu4e0eu5217u95f4u663eu793au5404u79cdu6570u636eu3002u672cu7bc7u5c06u5b66u4e60WPFu4e2du7684DataGrid u76f8u5173u529fu80fdu3002 
 ","link":"http://www.cnblogs.com/chillsrc/p/4881724.html","listurl":null,"view":"168","comment":"1","diggnum":"2","postdate":"2015-10-15 10:59","updated":"2015-10-15 07:25:58"},{"linkmd5id":"668996b90dc6901e3cd9b8a57c3053ec","title":"Kafkau9879u76eeu5b9eu6218uff0du7528u6237u65e5u5fd7u4e0au62a5u5b9eu65f6u7edfu8ba1u4e4bu7f16u7801u5b9eu8df5","desc":"
","link":"http://www.cnblogs.com/smartloli/p/4881690.html","listurl":null,"view":"173","comment":"1","diggnum":"2","postdate":"2015-10-15 10:50","updated":"2015-10-15 07:25:58"},{"linkmd5id":"c02f4087c3de0d74a1c4e63c2922f963","title":"u8be6u7ec6u89e3u8bf4u4effu5236QQu5217u8868 u5c55u5f00u548cu6536u8d77u5217u8868","desc":"
 u8fd1u6765u516cu53f8u7684u9879u76eeu8981u5199u4e2au7c7bu4f3cu4e8eQQu5217u8868u5c55u5f00u548cu6536u8d77u7684u5217u8868uff0cu60f3u5230u4ee5u524du674eu660eu6770u89c6u9891u91ccu8bf4u7684uff0cu5c31u5199u4e2au4e86u5c55u5f00u548cu6536u8d77u7684Demouff0cu5e0cu671bu7ed9u65b0u624bu4e00u70b9u601du8defuff0cu4e0eu5927u5bb6u5171u540cu8fdbu6b65u6211u5199u7684u8fd9u4e2au5c55u5f00u548cu6536u8d77u7684u601du8defu662fuff0cu7528u4e00u4e2au5b57u5178u7684Keyu53bbu8bb0u5f55u70b9u51fbu4e86u7b2cu51e0u7ec4uff0cu7528u72b6u60010-1u53bbu8bb0u5f55u4ed6u4eecu662fu5c55u5f00u7684u8fd8u662fu6536u8d77u7684u5e9fu8bddu4e0du591au8bf4uff0cu4e0au4ee3u7801uff0cu6ce8u91cau5df2u5199uff0cu4e00u5b9au8981u8ba4u771fu770bu54e6uff0cu8ba4u771f... 
 ","link":"http://www.cnblogs.com/bcblogs/p/4881612.html","listurl":null,"view":"449","comment":"2","diggnum":"2","postdate":"2015-10-15 10:27","updated":"2015-10-15 07:25:58"}]

  2、获取json对象数组

        String jsonUrArrUrl = "http://152.123.55.102:8080/index.php";
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
                jsonUrArrUrl, null, new Response.Listener<JSONArray>(){

            @Override
            public void onResponse(JSONArray response) {
                System.out.println("json data===" + response);
            }
        }, new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("发生了一个错误!");
                error.printStackTrace();
            }
        });
        requestQueue.add(jsonArrayRequest);

  3、运行结果

10-19 16:34:31.046  24583-24583/com.example.news.andoridnewsapp I/System.out﹕ json data===[{"diggnum":"0","postdate":"2015-10-15 12:59","title":"【原创】ZeroClipboard的时代或许已经过去了","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/mawuhen/p/4882104.html","view":"149","listurl":null,"comment":"1","linkmd5id":"c07e1ddae86029dbd1f4fb79a6f532fc"},{"diggnum":"6","postdate":"2015-10-15 12:38","title":"Web APi之捕获请求原始内容的实现方法以及接受POST请求多个参数多种解决方案(十四)","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/CreateMyself/p/4874273.html","view":"165","listurl":null,"comment":"5","linkmd5id":"3bf8dd1f81a47a83895e8c55669c6631"},{"diggnum":"0","postdate":"2015-10-15 12:31","title":"SharePoint API测试系列——Records.BypassLocks测试","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/LanTianYou/p/4882050.html","view":"50","listurl":null,"comment":"0","linkmd5id":"39375a02b70c6b0a395f42bd97c60cdb"},{"diggnum":"0","postdate":"2015-10-15 11:47","title":"C#委托使用详解(Delegates)","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/liuhaorain/p/3911845.html","view":"226","listurl":null,"comment":"1","linkmd5id":"be0e6182fb6fb216e58d21f08e54278e"},{"diggnum":"0","postdate":"2015-10-15 11:25","title":"golang reflect","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/coder2012/p/4881854.html","view":"86","listurl":null,"comment":"0","linkmd5id":"d85434e0d6e088a972f53447bf769256"},{"diggnum":"0","postdate":"2015-10-15 11:08","title":"Android Bitmap实战技巧","desc":"
    注:本文大量参考谷歌官方文档自http://developer.android.com/intl/zh-cn/training/displaying-bitmaps/index.html。如果你自学能力还可以或者英文理解能力不错可以直接去看原版的。如果你时间宝贵,想直接看结论和我个人理解的心得,也可... 
    ","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/punkisnotdead/p/4881771.html","view":"116","listurl":null,"comment":"1","linkmd5id":"9e6d443d4c2f489179c22830437e8034"},{"diggnum":"19","postdate":"2015-10-15 11:05","title":"【组件化开发】前端进阶篇之如何编写可维护可升级的代码","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/yexiaochai/p/4876099.html","view":"510","listurl":null,"comment":"7","linkmd5id":"71bf86ad9eb586aecdca37b9df43acb0"},{"diggnum":"2","postdate":"2015-10-15 10:59","title":"WPF入门教程系列二十一——DataGrid示例(一)","desc":"
    前面我们学习了ListView控件的使用示例,今天我们来学习DataGrid的有关知识。提到DataGrid 不管是Asp.Net中的网页开发还是WinForm应用程序开发都会频繁使用。通过它我们可以灵活的在行与列间显示各种数据。本篇将学习WPF中的DataGrid 相关功能。 
    ","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/chillsrc/p/4881724.html","view":"168","listurl":null,"comment":"1","linkmd5id":"794568d4689bf1c3fc5b6de7945cd978"},{"diggnum":"2","postdate":"2015-10-15 10:50","title":"Kafka项目实战-用户日志上报实时统计之编码实践","desc":"
","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/smartloli/p/4881690.html","view":"173","listurl":null,"comment":"1","linkmd5id":"668996b90dc6901e3cd9b8a57c3053ec"},{"diggnum":"2","postdate":"2015-10-15 10:27","title":"详细解说仿制QQ列表 展开和收起列表","desc":"
    近来公司的项目要写个类似于QQ列表展开和收起的列表,想到以前李明杰视频里说的,就写个了展开和收起的Demo,希望给新手一点思路,与大家共同进步我写的这个展开和收起的思路是,用一个字典的Key去记录点击了第几组,用状态0-1去记录他们是展开的还是收起的废话不多说,上代码,注释已写,一定要认真看哦,认真... 
    ","updated":"2015-10-15 07:25:58","link":"http://www.cnblogs.com/bcblogs/p/488

  这样便可方便地获取到json格式的数据。在此畅想一下,如果结合sqllite,是否可以异步获取差异数据进而以最小的流量获取更新的内容呢?

   更多官方例子可在此查看:https://developer.android.com/training/volley/index.html

原文地址:https://www.cnblogs.com/rwxwsblog/p/4892307.html