Android Network Open source framework Volley (v) some details of--volley

Source: Internet
Author: User

Recently took volley out to tidy up the next. Some of the minor problems that have not been encountered before have come again, in this record:


1, HttpURLConnection DELETE method can not add the body problem: Java.net.ProtocolException:DELETE does not support writing


This can be considered a system-level bug, why so, please look here, this problem in the java8 to be resolved. There is no way to go directly, we go around. Looking at HttpURLConnection, we find that he is an abstract class, so we can try to achieve our goal through other implementations of it. Eventually we decided to use the Okhttp implementation. The address is: Https://github.com/square/okhttp.

Then we have to look at volley source code, because our app is compatible with the minimum version is 4.0, so we know that the final call is Hurlstack:

    public static Requestqueue Newrequestqueue (context context, Httpstack stack) {...        if (stack = = null) {            if (Build.VERSION.SDK_INT >= 9) {                stack = new Hurlstack ();            } else {                //Prior to Gi Ngerbread, HttpURLConnection was unreliable.                see:http://android-developers.blogspot.com/2011/09/androids-http-clients.html                stack = new HttpClientStack ( Androidhttpclient.newinstance (useragent));            }        } ...    }

So we just need to modify the relevant code of Hurlstack, as follows:

Volley.java

public static Requestqueue Newrequestqueue (context context, Httpstack stack) {...        if (stack = = null) {            if (Build.VERSION.SDK_INT >= 9) {                //Old way:stack = new Hurlstack ();            http://square.github.io/okhttp/                stack = new Hurlstack (null, NULL, new Okurlfactory (New Okhttpclient ()));            } else {                //Prior to Gingerbread, httpurlconnection is unreliable.                see:http://android-developers.blogspot.com/2011/09/androids-http-clients.html                stack = new HttpClientStack ( Androidhttpclient.newinstance (useragent));            }        } ...    }
Hurlstack.java

/** * A {@link Httpstack} based on {@link httpurlconnection}.     */public class Hurlstack implements Httpstack {private final okurlfactory mokurlfactory; /** * @param urlrewriter Rewriter to use as Request URLs * @param sslsocketfactory SSL factory to use for HTTPS C Onnections * @param okurlfactory Solution Delete Body (https://github.com/square/okhttp) */Public Hurlstack (URL        Rewriter Urlrewriter, Sslsocketfactory sslsocketfactory, Okurlfactory okurlfactory) {murlrewriter = UrlRewriter;        Msslsocketfactory = sslsocketfactory;    Mokurlfactory = okurlfactory;     }/** * Create a {@link httpurlconnection} for the specified {@code URL}. */protected httpurlconnection createconnection (url url) throws IOException {if (null! = mokurlfactory) {return MOKURLFAC        Tory.open (URL);}    Return (HttpURLConnection) url.openconnection (); } @SuppressWarnings ("deprecation")/* Package */static void Setconnectionparametersforrequest (HTtpurlconnection connection, request<?> Request) throws IOException, authfailureerror {switch (re                Quest.getmethod ()) {... case Method.DELETE:connection.setRequestMethod ("DELETE");                Addbodyifexists (connection, request);            Break        ... default:throw new IllegalStateException ("Unknown method type."); }    }...}


2, about (modify) the cache of volley


Volley has a complete set of caching mechanisms. And now we want to do a simple need: some of the interface (almost will not change) to do a certain amount of time cache, study the next code found very easy to modify to achieve their own purposes (there is time in the analysis of the volley cache mechanism, this must be done). In simple terms, modify a place: request. Parsenetworkresponse in the

Httpheaderparser (here suddenly feeling volley design TMD flexible, how to change how to change). Httpheaderparser Modified code is as follows:

/** * Modified, User processing cache */public class Bhhttpheaderparser {/** * Extracts a {@link cache.entry} from a {@link networkres     Ponse}. * * @param response the network response to parse headers from * @return A cache entry for the given response, or     Null if the response is not cacheable. */public static cache.entry Parsecacheheaders (Networkresponse response, Boolean Iscustomcache) {... if (iscustom        Cache) {Softexpire = now + Config.http_cache_ttl;        } else {if (Hascachecontrol) {Softexpire = now + maxAge * 1000;  } else if (serverdate > 0 && serverexpires >= serverdate) {//Default semantic for Expire header            In HTTP specification is softexpire.        Softexpire = Now + (serverexpires-serverdate);        }} cache.entry Entry = new Cache.entry ();        Entry.data = Response.data;        Entry.etag = Serveretag;        Entry.softttl = Softexpire; Entry.ttl = ENTRY.SOFTTTl        Entry.serverdate = serverdate;        Entry.responseheaders = headers;    return entry; }...}
As you can see here, we mainly decide how to modify the cache TTL to achieve our goal based on the custom variables.


Later there are other summaries of volley that are recorded here.


Android Network Open source framework Volley (v) some details of--volley

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.