Several post submission request modes for Android volley framework

Source: Internet
Author: User

First of all, a brief description of Google's Android development team launched in 2013, a network communication framework volley. It is designed to carry out a small amount of data, but frequent communication network operation, and for large data volume network operations, such as downloading files, volley performance is not satisfactory.

In app development, the most common thing we do is send an HTTP request to the server from Appclient. For the two main Web request modes get and post, the Get request method is relatively simple to compare, Skip the table here. This paper mainly describes the way of several post submissions through volley.

1.client is submitted as normal post, and the server returns a string

Requestqueue requestqueue = Volley.newrequestqueue (Getapplicationcontext ()); Stringrequest stringrequest = new Stringrequest (Request.method.post,httpurl,    new response.listener<string > () {        @Override public        void Onresponse (String response) {            log.d (TAG, "response" + response);        }    }, new Response.errorlistener () {        @Override public        void Onerrorresponse (volleyerror error) {            LOG.E ( TAG, Error.getmessage (), error);        }    ) {    @Override    protected map<string, string> Getparams () {        //To set the number of references required to post              map<string, string> map = new hashmap<string, string> ();              Map.put ("Name1", "value1");              Map.put ("Name2", "value2");            return params;    };        Requestqueue.add (stringrequest);
2.client is submitted as a POST request of the JSON string, and the server returns the JSON string

Requestqueue requestqueue = Volley.newrequestqueue (Getapplicationcontext ());  map<string, string> map = new hashmap<string, string> ();  Map.put ("Name1", "value1");  Map.put ("Name2", "value2"); Jsonobject jsonobject = new Jsonobject (params); jsonrequest<jsonobject> jsonrequest = new Jsonobjectrequest (Method.post,httpurl, JSONObject, new Response.listener<jsonobject> () {@Override public void Onresponse (Jsonobject Response) {L        OG.D (TAG, "response" + response.tostring ());            }}, new Response.errorlistener () {@Override public void Onerrorresponse (Volleyerror error) {    LOG.E (TAG, Error.getmessage (), error);    }}) {//Note here The Getparams () method of the override here, setting the number of posts to be submitted at this point does not work at all//must be the same as above, constituting jsonobject as an actual participant in the Jsonobjectrequest object So this method is not required here//@Override//protected Map<string, string> Getparams () {//map< String, string> map = new hashmap<string, StrinG> ();  Map.put ("Name1", "value1");                  Map.put ("Name2", "value2"); Return params;//} @Override public map<string, string> getheaders () {HashMap        <string, string> headers = new hashmap<string, string> ();        Headers.put ("Accept", "Application/json"); Headers.put ("Content-type", "Application/json;                        Charset=utf-8 ");    return headers; }};requestqueue.add (jsonrequest);
Look at the above code, will think that volley this framework is not good enough, using the Jsonobjectrequest object to submit a POST request, assuming that there are parameters to commit, it must be submitted in Jsonobject JSON string.
Suppose the server does not support such a way? For example, the common spring MVC server, it is very difficult to support the JSON request method.
So if we want to achieve this goal, we need to use the methods given below.

3.client is submitted as a normal post, and the server returns a JSON string
First, in the activity class, inherit the request to implement a Normalpostrequest class.
Private class Normalpostrequest extends Request<jsonobject> {private map<string, string> MMap;    Private listener<jsonobject> Mlistener; Public normalpostrequest (String URL, listener<jsonobject> listener,errorlistener errorlistener, Map<String,                    String> map) {super (Request.Method.POST, URL, errorlistener);        Mlistener = listener;    MMap = map;  }//mmap is an instance that has been set up according to the previous method @Override protected map<string, string> Getparams () throws Authfailureerror    {return mMap; }//here because the Response return value requires JSON data, as with the Jsonobjectrequest class, you can @Override protected response<jsonobject> PARSENETW Orkresponse (networkresponse response) {try {String jsonstring = new String (response.data,httpheaderpar                            Ser.parsecharset (response.headers));        Return response.success (New Jsonobject (jsonstring), Httpheaderparser.parsecacheheaders (Response)); } catch (UnsupportedencOdingexception e) {return response.error (new ParseError (e));        } catch (Jsonexception je) {return response.error (new ParseError (JE));    }} @Override protected void Deliverresponse (Jsonobject response) {mlistener.onresponse (response); }}
The next call method is almost identical to the previous one, generating a request instance and adding a queue to it.
Requestqueue requestqueue = Volley.newrequestqueue (Getapplicationcontext ()); request<jsonobject> request = new Normalpostrequest (Httpurl,    new response.listener<jsonobject> () {        @Override public        void Onresponse (jsonobject response) {            log.d (TAG, "response" + response.tostring ()        }    }, new Response.errorlistener () {@Override public        void Onerrorresponse (volleyerror error) {            log.e (TAG, Error.getmessage (), error)        ,}    }, params); Requestqueue.add (request);
The above code is tested in the Android 4.3 environment.

Several post submission request modes for Android volley framework

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.