Android volley get JSON-formatted data

Source: Internet
Author: User

To enable Android to quickly access the network and resolve common data formats, Google has launched the volley library for the network transmission of Android systems. The volley library makes it easy to get pictures of remote servers, strings, JSON objects and arrays of JSON objects. Of course, Java itself also has a way to get JSON objects, but in order to better adapt to the mobile Internet, Google specifically for its special optimization, so should use the volley library as much as possible.

Volley Official document: https://developer.android.com/training/volley/index.html

Volley library Download: Git clone https://android.googlesource.com/platform/frameworks/volley/(due to Google, you need to do some special processing You can refer to the previous article "Git Cannot clone remote code base and git proxy settings ")

Environment: Android Studio

first, the volley library into the Android project (here is the way to import the source code, more ways to import the way to refer to the " Introduction to Android Studio and importing jar packages and third-party open Source Library Methods "):

1, the download good source copy to the app peer directory, under Setting.gradle add Volley library support

Include ': App ', ': Volley '

2. Add under Dependencies in App/build.gradle

Compile project (': Volley ')

The final results are as follows:

dependencies {    compile filetree (include: [' *.jar '], dir: ' Libs ')    ' Com.android.support: appcompat-v7:23.0.1 '    compile project (': Volley ')}

So far, volley trousers have been added to the project. Here's a look at how to properly read JSON objects and JSON arrays on a remote server through the volley library.

  Network permissions need to be set because you want to access the network. Add the following permission settings in Androidmanifest.xml.

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

Second, get the server JSON object

1. JSON object data format in server

{"Linkmd5id": "C07E1DDAE86029DBD1F4FB79A6F532FC", "View": "149", "comment": "1", "Diggnum": "0", "postdate": " 2015-10-15 12:59 "," Updated ":" 2015-10-15 07:25:58 "}

2. Get the JSON object

Requestqueue Requestqueue = Volley.newrequestqueue ( This); String Jsonurl= "Http://152.123.55.102:8080/json.html"; Jsonobjectrequest jsonobjectrequest=Newjsonobjectrequest (Request.Method.GET, Jsonurl,NULL,NewResponse.listener<jsonobject>() {@Override Public voidOnresponse (jsonobject response) {System.out.println ("JSON data===" +response); }        }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {System.out.println ("There was an error!" ");            Error.printstacktrace ();        }        }); Requestqueue.add (jsonobjectrequest);

3. Operation Result:

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 "}

Iii. getting an array of JSON objects

1. Original array

[{"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": "" "," 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": "Comment", "the": "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": "Comment", "the": "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": "+", "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. Get an array of JSON objects

String Jsonurarrurl = "http://152.123.55.102:8080/index.php"; Jsonarrayrequest jsonarrayrequest=Newjsonarrayrequest (Request.Method.GET, Jsonurarrurl,NULL,NewResponse.listener<jsonarray>() {@Override Public voidOnresponse (Jsonarray response) {System.out.println ("JSON data===" +response); }        }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {System.out.println ("There was an error!" ");            Error.printstacktrace ();        }        }); Requestqueue.add (jsonarrayrequest);

3. Operation Result

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": "Original" Zeroclipboard era may have passed, "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 ":" The Implementation method of the original content of the capture request of the Web API and the acceptance of multiple parameters of the POST request solution (14) "," 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 Test series--records.bypasslocks test "," desc ":" \ n "," updated ":" 2015-10-15 07:25:58 "," link ":" http:\/\/ Www.cnblogs.com\/lantianyou\/p\/4882050.html "," View ":" "," "listURL":NULL, "comment": "0", "linkmd5id": "39375a02b70c6b0a395f42bd97c60cdb"},{"diggnum": "0", "postdate": "2015-10-15 11:47", " Title ":" C # Delegate use detailed (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 ":" "," "listURL":NULL, "comment": "0", "linkmd5id": "d85434e0d6e088a972f53447bf769256"},{"diggnum": "0", "postdate": "2015-10-15 11:08", " Title ":" Android Bitmap Combat Skills "," desc ":" \ n Note: This article a lot of reference to Google official documents from the http:\/\/developer.android.com\/intl\/zh-cn\/training\/ Displaying-bitmaps\/index.html. If you are able to learn by yourself or you can understand English well, you can go straight to the original. If your time is precious, you want to look directly at the conclusion and my personal understanding of the experience, but also ... \ n "," updated ":" 2015-10-15 07:25:58 "," link ":" http:\/\/www.cnblogs.com\/ Punkisnotdead\/p\/4881771.html "," View ":" listURL "," the ":NULL, "comment": "1", "Linkmd5id": "9e6d443d4c2f489179c22830437e8034"},{"Diggnum": "+", "postdate": "2015-10-15 11:05", " Title ":" "Component development" front-end advanced article how to write can maintain upgradeable code "," 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 Getting Started Tutorial series 21--datagrid Example (i) "," desc ":" \ n before we learned the use of the ListView control example, today we will learn about the DataGrid. It is mentioned that the DataGrid is frequently used either in ASP. NET Web page development or WinForm application development. It allows us to flexibly display various data between rows and columns. This article will learn about the DataGrid-related features in WPF. \ 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 Project-real-time statistics of user log Reporting coding practice "," 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 ":" Detailed description of the Imitation QQ list expansion and the list "," desc ": \ n recently the company's project to write a list similar to the QQ list expansion and collection, think of the previous Li Mingjie Video said, wrote a launch and the collection of the demo, hoping to give the novice a little idea, With everyone to progress I wrote this development and the idea is to use a dictionary key to record click on the group, with the status of 0-1 to record whether they are expanded or collected nonsense not to say, on the code, comments have been written, must seriously look oh, serious ... \ n "," Updated ":" 2015-10-15 07:25:58 "," link ":" http:\/\/www.cnblogs.com\/bcblogs\/p\/488

This makes it easy to get data in JSON format. Imagine if, in combination with Sqllite, you can get the difference data asynchronously and get the updated content with minimal traffic?

More official examples can be viewed here: https://developer.android.com/training/volley/index.html

Android volley get JSON-formatted data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.