Volley, one of the open-source android Network frameworks

Source: Internet
Author: User

Google released the open-source network framework volley at the Google I/O May Developer Conference in 2013. I heard that the response was very good, so I decided to spend some time studying it.

1. What does Volley do?

Volley is a library that makes networking for Android apps easier and most importantly, faster. (From: https://developers.google.com/events/io/sessions/325304728 ). This sentence is still abstract. It is faster and easier to reflect in what aspects and when to use it? We need to learn more about this ~~

2. Start with obtaining

Volley ):

git clone https://android.googlesource.com/platform/frameworks/volley

After downloading it, You can import it as a library to your project, or package it and import it to your project so that we can use it. The source code and generated jar files can be downloaded here.

3. A simple demovolley usage process is roughly like this. First, we get the volley Request queue, then create our own request object, and then put it in the Request queue. What then? Then wait for the request result ~~ Is it too simple? It's actually that simple. So go directly to the Code:
Public class MainActivity extends Activity implements OnClickListener {private static final String URL = "http://www.baidu.com/"; private RequestQueue mQueue; // Request queue for volley @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // get the Request queue in this way. Of course, we will not do this in actual use. We usually use mQueue = Volley as a global variable. newRequestQueue (getApplicationCon Text () ;}@ Overridepublic void onClick (View v) {volleyRequest ();}/*** create a request, here we will do the simplest operation to request the webpage source code through GET. Print the result after the request is successful. */Private void volleyRequest () {StringRequest request = new StringRequest (Method. GET, URL, new Listener
() {@ Overridepublic void onResponse (String arg0) {Toast. makeText (getApplicationContext (), arg0, Toast. LENGTH_LONG ). show (); Log. d ("onResponse", arg0) ;}}, new ErrorListener () {@ Overridepublic void onErrorResponse (VolleyError arg0) {Toast. makeText (getApplicationContext (), arg0.toString (), Toast. LENGTH_LONG ). show (); Log. d ("onErrorResponse", arg0.toString () ;}}; mQueue. add (request );}}

4. Summary

I believe you have a rough understanding of this process. It is indeed very simple. The desired results come out, and you have not created a subthread in the main thread to run the request code. You may have some questions. The GET request transmits parameters through the URL. we can add the parameters directly after the string. What about the POST request? Is volley just like this? Is there anything else? What are her advantages? Haha. I will talk about this in this article. I will continue in the next article ~~

At last, I want to add the volley video and a PDF (that is, the content on the top page). Download here: http://pan.baidu.com/s/1ddyz23v.

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.