Android Volley framework (I), androidvolley

Source: Internet
Author: User

Android Volley framework (I), androidvolley
Use of the Android Volley framework (1)

As this part contains a lot of content, I plan to write it into several parts. This article mainly introduces the basic use of the Volley framework as a whole;

Why use the Volley framework?

Network operations are often required to develop android applications. The Android SDK providesHttpClientAndHttpUrlConnectionThe two methods are used to process network operations, but when the application is complicated, we need to write a lot of code to process a lot of things: Image caching, request scheduling, and so on;

The Volley framework was born to solve these problems. It was proposed at the Google I/O conference in 2013: Making network operations for Android applications more convenient and convenient; abstract implementation details such as the underlying Http Client, allowing developers to focus more on and generate RESTful requests. In addition, Volley asynchronously executes all requests on different threads to avoid blocking the main thread.

What are the characteristics of Volley?

  • Automatically schedule network requests
  • Multiple concurrent Network Connections
  • Use the standard HTTP cache mechanism to ensure the disk and memory response are consistent.
  • Supported Request priority
  • Supports powerful APIs for canceling requests. You can cancel a single request or multiple APIs.
  • Easy to customize
  • Robustness: easy to correctly update the UI and obtain data
  • Debugging and tracing tools
How to Use Volley

Obtain Volley:
-Git cloneHttps://android.googlesource.com/platform/frameworks/volley
Or https://github.com/mcxiaoke/android-volley
You can also download volley. jar directly.
-If you useGit cloneTo generatevoller.jarRun the following command in the volley directory:

android update project -pant jar

Add Volley to the project:
-Setvolley.jarPaste inlibsFolder, right-clickvolley.jarFile, selectAdd as Library

RequestQueue and Request in Volley

It is not difficult to see the literal meaning.

  • RequestQueueRequest queue used to execute requests
  • RequestUsed to construct a request object
  • Request objectThere are mainly the following types:
    • StringRequestThe response body is a string
    • JsonArrayRequestJSON array for sending and receiving
    • JsonObjectRequestSend and receive JSON objects
    • ImageRequestSend and receive images
Basic use of Volley

First, we need to createRequestQueue reqQueueAnd then buildXXRequest req, And then passreqQueue.add(req); Add the request to the Request queue;

Build RequestQueue
RequestQueue requestQueue = Volley. newRequestQueue (this); // here this refers to Context
Create Request(Take JsonObjectRequest as an example)
Private final String url = "http://www.xxx.com/person.json" // required url JsonObjectRequest req = new JsonObjectRequest (url, null, new Response. listener <JsonObject> () {@ Override public void onResponse (JsonObject response) {// Add your own response logic, }}, new ResponseError. listener () {@ Override public void onResponseError (VollerError error) {// handle the error Log. d ("Error Message:", "Error is" + error );}});
Add request to requestQueue
    requestQueue.add(req);

In buildingJsonObjectRequestFour parameters are required.

JsonObjectRequest req = new JsonObjectRequest (String url, Request. Method. GET (or POST), new Response. Listener (), new ResponseError. Listener ()),

The second parameter indicates the http method. The third and fourth parameters are the response listener and the response error listener, which must be overwritten respectively.onResponse()AndonResponseError()Method;RequestQueueThe request will be executed and the response callback will be made.onResponse()Method, so you need to implement your own business logic in the onResponse () method;

For more information aboutVolleyFollow your blog or Weibo posts and update them later!

  • Weibo: @ mingsang Android Black history,
  • Mailbox: <13141459344@163.com>
  • Personal Homepage: Coder: programmer & Engineering male daily,

Related Article

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.