Android Network Framework Volley

Source: Internet
Author: User

Volley Introduction

In the usual development process, our application almost always in the network, and the network programming under Android is generally based on the HTTP protocol, the common is the HttpURLConnection and httpclient two classes. Because of the use of more, it is easy to write some repetitive code, so there are some better network framework, such as Asynchttpclient, Xutils,universal-image-loader (network picture loading framework) and so on.

Volley is a new network framework that Google exited at the 2013 I/O conference, with volley ideal for network operations where data is small but frequently communicated. It's better to download or use Xutils.

Volley jar Package Download

Given the difficulty of the domestic Sisu network, you can download it on GitHub, Https://github.com/mcxiaoke/android-volley an unofficial package that is kept in sync with the official

Import the jar package into the Lib directory, and the preparation is done.

Volley use

Stringrequest

This class can be used to fetch a string from the server

Stringrequest stringrequest =NewStringrequest ("http://www.baidu.com",                          NewResponse.listener<string>() {@Override Public voidOnresponse (String response) {LOG.D ("TAG", response); }                          }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {LOG.E ("TAG", Error.getmessage (), error); }                          });
Applicationcontroller.getinstance (). Addtorequestqueue (Stringrequest);

Add Network Permissions

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

This code, is the Baidu home page of the HTML code to the request came over.

Requestqueue initialization Process

 Public requestqueue Getrequestqueue () {      //  Lazy Initialize the request queue, the queue instance'll Be      //  created when it's accessed  for the first time    ifnull c12>) {          = volley.newrequestqueue (Getapplicationcontext ());      }         return mrequestqueue;  }

The use of httpurlconnection after android2.3 before the httpclient,2.3 is also reflected here

Part of the source code in Newrequestqueue (context context, Httpstack stack):

if (Build.VERSION.SDK_INT >= 9) {              new  hurlstack ();           Else {              //  Prior to Gingerbread, httpurlconnection is unreliable.               //  http://android-developers.blogspot.com/2011/09/androids-http-clients.html              New  Httpclientstack (androidhttpclient.newinstance (useragent));          }

Android Network Framework Volley

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.