Android Volley获取json格式的数据

时间:2022-09-27 21:37:56

  为了让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":"\u3010\u539f\u521b\u3011ZeroClipboard\u7684\u65f6\u4ee3\u6216\u8bb8\u5df2\u7ecf\u8fc7\u53bb\u4e86","desc":"\n","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 APi\u4e4b\u6355\u83b7\u8bf7\u6c42\u539f\u59cb\u5185\u5bb9\u7684\u5b9e\u73b0\u65b9\u6cd5\u4ee5\u53ca\u63a5\u53d7POST\u8bf7\u6c42\u591a\u4e2a\u53c2\u6570\u591a\u79cd\u89e3\u51b3\u65b9\u6848\uff08\u5341\u56db\uff09","desc":"\n","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 API\u6d4b\u8bd5\u7cfb\u5217\u2014\u2014Records.BypassLocks\u6d4b\u8bd5","desc":"\n","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#\u59d4\u6258\u4f7f\u7528\u8be6\u89e3\uff08Delegates\uff09","desc":"\n","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":"\n","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 Bitmap\u5b9e\u6218\u6280\u5de7","desc":"\n \u6ce8\uff1a\u672c\u6587\u5927\u91cf\u53c2\u8003\u8c37\u6b4c\u5b98\u65b9\u6587\u6863\u81eahttp:\/\/developer.android.com\/intl\/zh-cn\/training\/displaying-bitmaps\/index.html\u3002\u5982\u679c\u4f60\u81ea\u5b66\u80fd\u529b\u8fd8\u53ef\u4ee5\u6216\u8005\u82f1\u6587\u7406\u89e3\u80fd\u529b\u4e0d\u9519\u53ef\u4ee5\u76f4\u63a5\u53bb\u770b\u539f\u7248\u7684\u3002\u5982\u679c\u4f60\u65f6\u95f4\u5b9d\u8d35\uff0c\u60f3\u76f4\u63a5\u770b\u7ed3\u8bba\u548c\u6211\u4e2a\u4eba\u7406\u89e3\u7684\u5fc3\u5f97\uff0c\u4e5f\u53ef... \n ","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":"\u3010\u7ec4\u4ef6\u5316\u5f00\u53d1\u3011\u524d\u7aef\u8fdb\u9636\u7bc7\u4e4b\u5982\u4f55\u7f16\u5199\u53ef\u7ef4\u62a4\u53ef\u5347\u7ea7\u7684\u4ee3\u7801","desc":"\n","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":"WPF\u5165\u95e8\u6559\u7a0b\u7cfb\u5217\u4e8c\u5341\u4e00\u2014\u2014DataGrid\u793a\u4f8b(\u4e00)","desc":"\n \u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86ListView\u63a7\u4ef6\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u4eca\u5929\u6211\u4eec\u6765\u5b66\u4e60DataGrid\u7684\u6709\u5173\u77e5\u8bc6\u3002\u63d0\u5230DataGrid \u4e0d\u7ba1\u662fAsp.Net\u4e2d\u7684\u7f51\u9875\u5f00\u53d1\u8fd8\u662fWinForm\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u90fd\u4f1a\u9891\u7e41\u4f7f\u7528\u3002\u901a\u8fc7\u5b83\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u7684\u5728\u884c\u4e0e\u5217\u95f4\u663e\u793a\u5404\u79cd\u6570\u636e\u3002\u672c\u7bc7\u5c06\u5b66\u4e60WPF\u4e2d\u7684DataGrid \u76f8\u5173\u529f\u80fd\u3002 \n ","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":"Kafka\u9879\u76ee\u5b9e\u6218\uff0d\u7528\u6237\u65e5\u5fd7\u4e0a\u62a5\u5b9e\u65f6\u7edf\u8ba1\u4e4b\u7f16\u7801\u5b9e\u8df5","desc":"\n","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":"\u8be6\u7ec6\u89e3\u8bf4\u4eff\u5236QQ\u5217\u8868 \u5c55\u5f00\u548c\u6536\u8d77\u5217\u8868","desc":"\n \u8fd1\u6765\u516c\u53f8\u7684\u9879\u76ee\u8981\u5199\u4e2a\u7c7b\u4f3c\u4e8eQQ\u5217\u8868\u5c55\u5f00\u548c\u6536\u8d77\u7684\u5217\u8868\uff0c\u60f3\u5230\u4ee5\u524d\u674e\u660e\u6770\u89c6\u9891\u91cc\u8bf4\u7684\uff0c\u5c31\u5199\u4e2a\u4e86\u5c55\u5f00\u548c\u6536\u8d77\u7684Demo\uff0c\u5e0c\u671b\u7ed9\u65b0\u624b\u4e00\u70b9\u601d\u8def\uff0c\u4e0e\u5927\u5bb6\u5171\u540c\u8fdb\u6b65\u6211\u5199\u7684\u8fd9\u4e2a\u5c55\u5f00\u548c\u6536\u8d77\u7684\u601d\u8def\u662f\uff0c\u7528\u4e00\u4e2a\u5b57\u5178\u7684Key\u53bb\u8bb0\u5f55\u70b9\u51fb\u4e86\u7b2c\u51e0\u7ec4\uff0c\u7528\u72b6\u60010-1\u53bb\u8bb0\u5f55\u4ed6\u4eec\u662f\u5c55\u5f00\u7684\u8fd8\u662f\u6536\u8d77\u7684\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u4e0a\u4ee3\u7801\uff0c\u6ce8\u91ca\u5df2\u5199\uff0c\u4e00\u5b9a\u8981\u8ba4\u771f\u770b\u54e6\uff0c\u8ba4\u771f... \n ","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":"\n","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":"\n","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":"\n","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":"\n","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":"\n","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":"\n    注:本文大量参考谷歌官方文档自http:\/\/developer.android.com\/intl\/zh-cn\/training\/displaying-bitmaps\/index.html。如果你自学能力还可以或者英文理解能力不错可以直接去看原版的。如果你时间宝贵,想直接看结论和我个人理解的心得,也可... \n    ","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":"\n","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":"\n    前面我们学习了ListView控件的使用示例,今天我们来学习DataGrid的有关知识。提到DataGrid 不管是Asp.Net中的网页开发还是WinForm应用程序开发都会频繁使用。通过它我们可以灵活的在行与列间显示各种数据。本篇将学习WPF中的DataGrid 相关功能。 \n    ","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":"\n","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":"\n    近来公司的项目要写个类似于QQ列表展开和收起的列表,想到以前李明杰视频里说的,就写个了展开和收起的Demo,希望给新手一点思路,与大家共同进步我写的这个展开和收起的思路是,用一个字典的Key去记录点击了第几组,用状态0-1去记录他们是展开的还是收起的废话不多说,上代码,注释已写,一定要认真看哦,认真... \n    ","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