Use of Android volley framework (i)

Source: Internet
Author: User

Why use the volley framework

Developing Android apps often involves network operations, and the Android SDK provides HttpClient and httpurlconnection two ways to handle network operations, But when the application is more complex, we need to write a lot of code to deal with a lot of things: image caching, request scheduling and so on;

The volley framework, which was created to address this, was presented at the 2013 Google I/O Conference: Making Android apps easier and faster, abstracting the details of implementations such as the underlying HTTP client, and allowing developers to focus and create restful Request. In addition, volley executes all requests asynchronously on different threads and avoids blocking the main thread.

What are the characteristics of volley?

    1. Automatic dispatch of network requests
    2. Multiple Concurrent Network Connections
    3. Keeping disk and memory responses consistent by using a standard HTTP caching mechanism
    4. Support Request Priority
    5. A powerful API that supports cancellation requests, which can cancel a single request or multiple
    6. Easy to customize
    7. Robustness: Easy to update UI and get data correctly
    8. Includes Debug and trace tools

How to use volley

- git clone Https://android.googlesource.com/platform/frameworks/volley or https://github.com/mcxiaoke/ Android-volley

Or go to maven to download Http://central.maven.org/maven2/com/mcxiaoke/volley/library/1.0.19/library-1.0.19.jar

Requestqueue and Request in the volley
    • RequestQueueThe request queue used to execute the request
    • RequestUsed to construct a request object
    • Request对象There are several main types:
      1. StringRequestThe body of the response is a string
      2. JsonArrayRequestSending and receiving JSON arrays
      3. JsonObjectRequestSending and receiving JSON objects
      4. ImageRequestSend and receive image
basic use of volley

First we need to create one RequestQueue requestQueue , then build one that we need XXRequest req , then pass requestQueue.add(req) ; Add the request to the request queue;

Build a Requestqueue
Requestqueue Requestqueue=volley.newrequestqueue (this);//This refers to the context
Create a Request(Take jsonobjectrequest as an example)
Private final String url= "http:/xxxxx"//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 err) {            //error handling            L.D ("Error Message:", "error is" +error);        }    });
Add req to Requestqueue

When building an JsonObjectRequest object, four parameters are required, where the second parameter represents the HTTP method, and the third and fourth are the response listener and the response error listener, respectively, to overwrite onResponse() and onResponseError() method; RequestQueue Will execute the request and will respond to the callback onResponse() method, so you need to implement your own business logic in the Onresponse () method

POST request

Http://www.2cto.com/kf/201408/324454.html

Use of Android volley framework (i)

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.