Use of Volley.jar

Source: Internet
Author: User

1.1 Volley Introduction

The 2013 Google I/O conference introduced a new network communication framework,--volley. Volley but the asynchttpclient and universal-image-loader of the advantages of the set in one, can be as very simple as asynchttpclient http communication, You can also load pictures on your network as easily as Universal-image-loader. In addition to simple and easy to use, volley in the performance of the large-scale adjustment, its design goal is very suitable for the volume of data is not small, but the traffic is frequent network operation, and for large data volume of network operations, such as downloading files, volley performance will be very bad.

Features provided by 1.2 volley
Simply put, it provides the following handy features:

Asynchronous download of JSON, image, etc.;

Ordering of network requests (scheduling)

Priority handling of network requests

Cache

Multi-level cancellation requests and activity and lifecycle linkage (all network requests are canceled at the end of activity)

1.3 Use of volley in this project

* Custom Request Queue Requestqueue

Package Com.genting.membership.singleton;

import Android.content.Context;

import com.android.volley.RequestQueue;

import Com.android.volley.toolbox.ImageLoader;

import Com.android.volley.toolbox.Volley;

import Com.googlecode.androidannotations.annotations.EBean;

import Com.googlecode.androidannotations.api.Scope;

import Com.macrokiosk.utilities.BitmapLruCache;

@EBean (scope = scope. Singleton)

Public class Volleysingleton {

Public Requestqueue Requestqueue;

Public Imageloader Imageloader;

Public Volleysingleton (Context context) {

if (context = null) {

Requestqueue = Volley. Newrequestqueue (context);

Imageloader = new imageloader (Requestqueue, new Bitmaplrucache ());

}

}

}

* Custom Task class: Jsonarrayrequest,stringrequest

Public class Jsonarrayrequest extends jsonrequest<jsonarray> {

Public Jsonarrayrequest (int method, String url,jsonobject jsonrequest,

Listener<jsonarray>listener, Errorlistener Errorlistener) {

Super (Method, url, (jsonrequest = = null)? null : Jsonrequest.tostring (),

Listener, Errorlistener);

}

@Override

protected Response<jsonarray> parsenetworkresponse (Networkresponse Response) {

Try {

Stringjsonstring =

New String (Response.data, Httpheaderparser. Parsecharset(response.headers));

return Response. Success (newJsonarray (jsonstring),

Httpheaderparser. parsecacheheaders (response));

} catch(Unsupportedencodingexception e) {

return Response. Error (new parseerror (e));

} Catch (jsonexception je) {

return Response. Error (new parseerror (JE));

}

}

@Override

Public Retrypolicy Getretrypolicy () {

retrypolicymretrypolicy=New defaultretrypolicy (60000, 1,1.0f);

return Mretrypolicy;

}

}

* Load task, send request

Jsonarrayrequest request=New jsonarrayrequest (Method). GET, Constants. basic_url,null, Geturlsuccess (), GetError ("Getbasicurl"));

Request.setretrypolicy (newdefaultretrypolicy (1, 1.0f));

Mrequestqueue.add (Request);

* Cancel Request Task

Soaprequest.settag (this), where this is the label of the set

MVolley.requestQueue.cancelAll (this), where this is the label just set

Use of Volley.jar

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.