Android advanced-volley-1. Using Volley

Source: Internet
Author: User

Reference Links:

Http://www.codekk.com/open-source-project-analysis/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90

Volley-demo:

Https://github.com/android-cn/android-open-project-demo/tree/master/volley-demo

1. Import Eclipse

After importing Volley-demo (. \android-open-project-demo-master\volley-demo) into Eclipse:eclipse->file->import-> Existingandroid Code into Workspace. After importing is a homeactivity, and there will be a heap of R cannot is resolved to a variable error, restart Eclipse can

2. Learning Volley-demo

2.1 Using Stringrequest

The following is the listener code for the Stringrequest send button:

Btnsend.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (Stringutil.isempty ( Eturl.gettext (). toString ())) {Toastutil.showtoast (getactivity (), "Please enter request Address"); return;} Before the request, cancel the previous request (cancel the request that has not yet completed) Volleyutil.getqueue (Getactivity ()). Cancelall (this); Tvresult.settext (""); Stringrequest request = new Stringrequest (Stringutil.preurl (Eturl.gettext (). toString (). Trim ()), New listener< String> () {@Overridepublic void Onresponse (String response) {Tvresult.settext (response);}}, New Errorlistener () {@ overridepublic void Onerrorresponse (Volleyerror arg0) {toastutil.showtoast (Getactivity (), Getresources (). GetString ( R.string.request_fail_text));}); /request plus tag for cancellation of request Request.settag (this); Volleyutil.getqueue (Getactivity ()). Add (Request);});

It can be seen from the code that it is primarily a new stringrequest, which is then added to the request queue Requestqueue through the Add function.

Next look at the Stringrequest constructor:

public class Stringrequest extends Request<string> {... Public stringrequest (String URL, listener<string> Listener, Errorlistener Errorlistener);.. }

Where request is an abstract class, listener is an interface. Request has a constructor function:

Public Request (int method, string URL, Response.errorlistener listener);p ublic request (string URL, Response.errorlistener listener) {This        (method.deprecated_get_or_post, URL, listener);    }

There are functions in listener:

Public Voidonresponse (T response);

This function is recalled when the system stringrequest processing is complete. In this demo, the system callback Onresponse, the received data is displayed on the TextView.

3. Summary

Using volley is simple, and the process can be divided into two steps:

    1. Create request queue Requestqueue RQ = new Volley.newrequestqueue ()
    2. Create a new request Xxrequest, and then add the request to the queue RQ: Rq.add (xxrequest);

Today first understand the preliminary use of volley, the next will be the volley source code for detailed analysis







Android advanced-volley-1. Using 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.