Volley usage, Volley usage

Source: Internet
Author: User

Volley usage, Volley usage

Volley's various Request usage methods, including StringRequest, JsonRequest, and ImageRequest. StringRequest is used to request a common text data, JsonRequest (JsonObjectRequest, JsonArrayRequest) is used to request a JSON data, and ImageRequest is used to request an image on the network.

Add in project Glide

compile 'com.mcxiaoke.volley:library:1.0.19'
StringRequest steps

1. Create a RequestQueue object.

2. Create a StringRequest object.

3. Add the StringRequest object to RequestQueue.

RequestQueue queue = Volley.newRequestQueue(AchieveDate.this);
StringRequest request=new StringRequest("https://www.baidu.com/", new Response.Listener
 
  () {    @Override    public void onResponse(String response) {        Log.d(
  TAG, "onResponse: " + response);    }}, new Response.ErrorListener() {    @Override    public void onErrorResponse(VolleyError error) {        Log.e(
  TAG, "onErrorResponse: " + error.getMessage(), error);    }});
 
queue.add(request);
The above is a GET and POST method of the HTTP request method. The method is

However, this only specifies that the HTTP request method is POST. How can we set the parameters to be submitted to the server? Unfortunately, the StringRequest does not provide a method to set the POST parameter. However, when a POST Request is sent, Volley tries to call the StringRequest parent class -- getParams () in the Request () method to obtain the POST parameter, then the solution will naturally be available. We only need to re-write the getParams () method in the anonymous class of StringRequest. Here we can set the POST parameter, the Code is as follows:

StringRequeststringRequest = newStringRequest (Method. POST, url, listener, errorListener );

StringRequeststringRequest = newStringRequest (Method. POST, url, listener, errorListener ){

@ Override

ProtectedMap GetParams () throwsAuthFailureError {

Map Map = newHashMap ();

Map. put ("params1", "value1 ");

Map. put ("params2", "value2 ");

Returnmap;

}

};

The implementation steps of JsonRequest (JsonObjectRequest and JsonArrayRequest) are the same as those of StringRequest ,,

ImageRequest

1. Create a RequestQueue object.

2. Create a Request object.

3. Add the Request object to RequestQueue.

ImageRequestimageRequest = newImageRequest (

"Https://developer.android.com/images/home/aw_dac.png ",

NewResponse. Listener (){

@ Override

PublicvoidonResponse (Bitmapresponse ){

ImageView. setImageBitmap (response );

}

}, 0, 0, Config. RGB_565, newResponse. ErrorListener (){

@ Override

PublicvoidonErrorResponse (VolleyErrorerror ){

ImageView. setImageResource (R. drawable. default_image );

}

});

ImageLoader

Since ImageLoader does not inherit from the Request, its usage is different from what we have learned. In summary, it can be divided into the following four steps:

1. Create a RequestQueue object.

2. Create an ImageLoader object.

3. Get an ImageListener object.

4. Call the get () method of ImageLoader to load images on the network.

ImageLoaderimageLoader = newImageLoader (mQueue, newImageCache (){

@ Override

PublicvoidputBitmap (Stringurl, Bitmapbitmap ){

}

@ Override

PublicBitmapgetBitmap (Stringurl ){

Returnnull;

}

});

ImageListenerlistener = ImageLoader. getImageListener (imageView,

R. drawable. default_image, R. drawable. failed_image );

NetworkImageView

1. Create a RequestQueue object.

2. Create an ImageLoader object.

3. Add a NetworkImageView control to the layout file.

4. Obtain the control instance in the code.

5. Set the image address to be loaded.

  
RequestQueue queue = Volley.newRequestQueue(AchieveDate.this);ImageLoader imageLoader = new ImageLoader(queue, new ImageCache());networkImageView = findViewById(R.id.image_view);networkImageView.setDefaultImageResId(R.drawable.ic_launcher_background);networkImageView.setErrorImageResId(R.drawable.image_default);networkImageView.setImageUrl("https://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", imageLoader);
public class ImageCache implements ImageLoader.ImageCache {    private LruCache
 
   cache;    public ImageCache() {        int maxSize = 10 * 1024 * 1024;        cache = new LruCache
  
   (maxSize) {            @Override            protected int sizeOf(String key, Bitmap value) {                return value.getRowBytes() * value.getHeight();            }        };    }    @Override    public Bitmap getBitmap(String url) {        return cache.get(url);    }    @Override    public void putBitmap(String url, Bitmap bitmap) {        cache.put(url, bitmap);    }}
  
 

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.