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 operations, and for large data volume of 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 the app client. For two basic Web requests, get and post, get requests are relatively straightforward, Skip the table here. This article focuses on how several post submissions are made through volley.

1. The client submits a 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 () {        //Set the parameter map<string that need post here              , string> map = new hashmap<string, string> ();              Map.put ("Name1", "value1");              Map.put ("Name2", "value2");            return params;    };        Requestqueue.add (stringrequest);
2. The client submits the JSON string as a POST request, 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 post required to submit the parameters at all does not work//must be as above, make up jsonobject as arguments in the Jsonobjectrequest object So this method is not needed 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);
Read the above code, will feel volley this framework is not perfect, use Jsonobjectrequest object to submit a POST request, if there are parameters need to commit, it must be submitted in Jsonobject JSON string.
What if the server does not support this approach? For example, the common spring MVC server, it is difficult to support the JSON request method.
Then we want to achieve this goal, we need to use the method given below.

3. The client submits a normal post, and the server returns the 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 of the parameter that has been set up in the previous way @Override protected map<string, string> Getparams () throws Authfailureerror    {return mMap; }//here because the Response return value requires JSON data, as with the Jsonobjectrequest class @Override protected response<jsonobject> parsenetwo Rkresponse (networkresponse response) {try {String jsonstring = new String (response.data,httpheaderpars                            Er.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 similar to the previous one, generating a request instance and joining the queue.
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 Android 4.3 environment.

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.