Android network communication Essentials artifact volley--Implement a custom request (take Gson as an example)

Source: Internet
Author: User

Although volley has provided for the parsing of string, picture, json,request, but there is always some data does not belong to the above three kinds of, this time we need to implement a request to resolve the data we need.

Implementing a custom request is simple:

1. Inheritance request<t>,t represents the type you want to parse, such as the response returned by the server is a String, then inherit request<string>

2. Implement the Parsenetworkresponse() and Deliverresponse() methods.

Parsenetworkresponse

@Overrideprotected response<t> parsenetworkresponse (        networkresponse Response) {    try {        String JSON = new String (Response.data,        httpheaderparser.parsecharset (response.headers));    Return response.success (Gson.fromjson (JSON, clazz),    httpheaderparser.parsecacheheaders (Response));    }    Handle Errors ...}


Parsenetworkresponse () parameter networkresponse, this parameter contains the HTTP status code, response header and other data. When we override this function, the returned response<t> must contain your typed return object, the cached metadata, or an error.

Volley is called Parsenetworkresponse in a new thread to prevent the main thread from clogging.


DeliverresponseVolley callback you in the main thread through the object you returned in Parsenetworkresponse (). Most of the request will trigger a callback interface in this method, for example
protected void Deliverresponse (T response) {        listener.onresponse (response);

Example: Gsonrequest
<span style= "FONT-SIZE:14PX;"    >public class Gsonrequest<t> extends request<t> {private final Gson Gson = new Gson ();    Private final class<t> Clazz;    Private final map<string, string> headers;    Private final listener<t> Listener;     /** * Make a GET request and return a parsed an object from JSON.  * * @param URL of the URL of the request to make * @param clazz relevant class object, for Gson ' s reflection * @param Headers Map of Request headers */public gsonrequest (String URL, class<t> clazz, map<string, string> H Eaders, listener<t> Listener, Errorlistener Errorlistener) {super (Method.get, URL, errorlistener)        ;        This.clazz = Clazz;        This.headers = headers;    This.listener = listener; } @Override public map<string, String> getheaders () throws Authfailureerror {return headers! = null?    Headers:super.getHeaders (); } @Override protected void deLiverresponse (T response) {listener.onresponse (response); } @Override protected response<t> parsenetworkresponse (Networkresponse Response) {try {Str ing json = new String (Response.data, Httpheaderparser.parsecharset (response.headers            )); Return response.success (Gson.fromjson (JSON, clazz), Httpheaderparser.parsecachehead        ERS (response));        } catch (Unsupportedencodingexception e) {return response.error (new ParseError (e));        } catch (Jsonsyntaxexception e) {return response.error (new ParseError (e)); }}}</span>


Copyright NOTICE: This article for Bo Master original article, reproduced please contact Bo Master.

Android network communication Essentials artifact volley--Implement a custom request (take Gson as an example)

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.