Android Volley basic use/set HTTP Request Parameters, apikey, and androidapikey

Source: Internet
Author: User

Android Volley basic use/set HTTP Request Parameters, apikey, and androidapikey

Recently I am working on an Android news client, and I feel that I have gained a lot.

Here is how Volley obtains network data.

Volley is a network communication Library launched by Google I/O 2013. It is highly scalable and can make the code more robust. Volley provides high-performance network communication functions while, it also provides good support for network image loading.

The following describes how to use Volley's StringRequest.

The simple syntax is as follows:

1/** 2 * Get network data news card 3 */4 private void getNewsData (String NewsType) {5 String URL = NewsType; // domestic news list 6 StringRequest stringRequest = new StringRequest (Request. method. GET, URL, new Response. listener <String> () {7 Gson gson = new Gson (); 8 9 @ Override10 public void onResponse (String response) {11 // obtain the string sent from the network. The operation is generally to convert to Json and other operations 12} 13}, new Response. errorListener () {14 15 @ Override16 public void onErrorResponse (VolleyError error) {17 // action to be performed when the request fails 18} 19}) {20 21 mQueue. add (stringRequest); // add to the Request queue. Otherwise, 22 mQueue of the request will not be sent. start (); 23}

Previously, the error "add ()" was not written, so the onresponse in the middle would not be executed.

The following describes how to add Request Parameters in Volley, such as apikey httpArg.

You need to override the getHeaders method:

1  @Override2     public Map<String, String> getHeaders() throws AuthFailureError3     {4     Map<String, String> headers = new HashMap<String, String>();5     headers.put("Charset", "UTF-8");6     headers.put("Content-Type", "application/x-javascript");7     headers.put("Accept-Encoding", "gzip,deflate");8     return headers;9     }

You can add any parameters you want here.

The following is an example:

1/** 2 * Get network data news card 3 */4 private void getNewsData (String NewsType) {5 String URL = NewsType; // domestic news list 6 StringRequest stringRequest = new StringRequest (Request. method. GET, URL, new Response. listener <String> () {7 Gson gson = new Gson (); 8 9 @ Override10 public void onResponse (String response) {11 NewsListResponseJson newsListJsonObject = gson. fromJson (response, NewsListResponseJson. class); 12 newsItems = newsListJsonObject. getData (); 13} 14}, new Response. errorListener () {15 16 @ Override17 public void onErrorResponse (VolleyError error) {18 19} 20}) {21 @ Override22 public Map <String, String> getHeaders () throws AuthFailureError {23 Map <String, String> headers = new HashMap <> (); 24 headers. put ("apikey", Constant. APIKET); 25 return headers; 26} 27}; 28 mQueue. add (stringRequest); 29 mQueue. start (); 30}

Note: (the image is obtained through picasso. For usage instructions, refer to the next article of the blogger)

It's so simple, it's not like writing a thread on your own, and repeat all kinds of work.

:

 

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.