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":"\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

时间: 2024-10-05 23:47:42

Android Volley获取json格式的数据的相关文章

C# 语言如何获取json格式的数据,不用javascript用c#实现。。。

{'state' : 1,'data':{'list':[{'id': 123, 'name': '诸葛天邪','level': 10,'country': 1,}]}} 比如我要获取里面的 id 该如何实现. 我有更好的答案 分享到: 按默认排序 | 按时间排序 2条回答 2012-06-25 20:15yzy_130 | 七级 最快回答 首先这个json串有几个错误的地方 第一个非常重要的错误,json串里面只能用双引号,不能用单引号,这个单双引号表示的意思不是引号内是字符串,而是json的

Spring拦截器从Request中获取Json格式的数据

内容如题 1.创建取值类 1 /** 2 * MyRequestWrapper.java 3 * 4 * @screen 5 * @author havery 6 */ 7 package com.newpp.core.interceptor; 8 9 import java.io.BufferedReader; 10 import java.io.ByteArrayInputStream; 11 import java.io.IOException; 12 import java.io.Inp

Android上解析Json格式数据

package com.practice.json; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class JsonDemo extends Activity { /*http://www.hui

jQuery获取JSON格式数据方法

getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ $.getJSON("demo_ajax_json.js",function(result_data){ $.each(result_data, function(index, name_value){ $("div").append(name_value + &qu

Android读写JSON格式的数据之JsonWriter和JsonReader

最近的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.最近打算找一份Android的工作,要继续拾起以前的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才开始办公.不说这些了,下面开始今天的正文. JSON格式的数据通常用于网络传输,尤其是在移动设备上与服务端进行数据交互.JSON与XML比较有明显的优势.XML格式的数据非常臃肿,传递少量的数据就要附带很多额外的格式数据.而JSON除了少量的符号外,全部是真正的数据,所以有越来越多的程序用JSON来取代XM

Android之使用json进行网络数据交换

JSON作为一种轻量级的数据交换格式,凭借其易于阅读和编写.易于解析.传输速度快等优点流行了起来.最近正好在学习Android端从服务端端取数据,Json便派上了用场.好,下面开始切入主题. 1.准备好服务端 首先我们要有个服务器,我用的是Tomcat7.0,这里具体Tomcat的安装过程我就不加以描述了.服务器配置过程如下. 我们在File-new -other中找到server,next然后在Apache中找到tomcat7.0,我这里将server的名字改为了Myserve,点击finis

ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理

话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性对token进行验证,并通过时间戳的方式统一处理接收与返回的时间格式. 请求Json格式: { "Cmd": "login", "Token": "", "PageNo": 0, "OnePageNu

PHP接收JSON格式的数据

在API服务中,目前流行采用json形式来交互. 给前端调用的接口输出Json数据,这个比较简单,只需要组织好数据,用json_encode($array) 转化一下,前端就得到json格式的数据. 接收前端提交的json数据稍微复杂一点,原因是:PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型.因此,对型如text/xml 或者 soap 或者 application/octet-stream 和application/json格式之类

javascript 解析ajax返回的xml和json格式的数据

写个例子,以备后用 一.JavaScript 解析返回的xml格式的数据: 1.javascript版本的ajax发送请求 (1).创建XMLHttpRequest对象,这个对象就是ajax请求的核心,是ajax请求和响应的信息载体,单是不同浏览器创建方式不同 (2).请求路径 (3).使用open方法绑定发送请求 (4).使用send() 方法发送请求 (5).获取服务器返回的字符串   xmlhttpRequest.responseText; (6).获取服务端返回的值,以xml对象的形式存