Use of Android volley framework (i)

Source: Internet
Author: User

Use of Android volley framework (i)

As this part of the content is more, so intends to be divided into several parts to write, this article is mainly from the overall introduction of the basic use of the volley framework;

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?

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

get Volley:
- git clone Https://android.googlesource.com/platform/frameworks/volley
or Https://github.com/mcxiaoke/android-volley.
You can also download Volley.jar directly
-If you are using git clone mode, in order to generate voller.jar you need to execute in the volley directory:

update project -pant jar

add volley to the project:
- volley.jar paste into libs folder, then right-click volley.jar file, select Add as Library

Requestqueue and Request in the volley

According to the literal meaning, it is easy to see

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

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

Build aRequestqueue
RequestQueue requestQueue=Volley.newRequestQueue(this);//这里的this指的是Context
Create aRequest(Take jsonobjectrequest as an example)
private final String url="http://www.xxx.com/person.json"//所需urlJsonObjectRequest req=new JsonObjectRequest(url,null,new Response.Listener<JsonObject>(){    @Override    public void onResponse(JsonObject response){        //添加自己的响应逻辑,    }},new ResponseError.Listener(){    @Override    public void onResponseError(VollerError error){        //错误处理        Log.d("Error Message:","Error is"+error);    }});
Add request to Requestqueue
requestQueue.add(req);

When building an JsonObjectRequest object, four parameters are required

new JsonObjectRequest(String url,Request.Method.GET(或者POST),newResponse.Listener(),new ResponseError.Listener()),

The second parameter represents the HTTP method, and the third and the fourth are the response listener and the response error listener, respectively, need 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;

For more information about the use of volley , please follow the personal blog or Weibo, then update!

    • Weibo: @ Ming sang Android Black History,
    • E-mail: <[email protected]>
    • Personal homepage: Coder: Programmers && Engineering men's Daily,

Use of Android volley framework (i)

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.