A convenience utility for interfacing with volley and submitting requests. Projectile is a simple to use request builder with a focus on easy configuration. Take a look below for some quick examples on how to get started.
Current version: 1.0.x
publicclassSampleActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){// boilerplate...Projectile.draw(this).aim("http://www.google.com") .fire(newResponseListener<String>(){@OverridepublicStringresponseParser(NetworkResponsenetworkResponse){Stringresponse; try{response = newString(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers))} catch (UnsupportedEncodingExceptione){response = newString(networkResponse.data)} returnresponse} @OverridepublicvoidonResponse(Stringresponse){Log.d(Sample.class.getName(), response)} @OverridepublicvoidonError(VolleyErrorerror){Log.d(Sample.class.getName(), "Error")} })} }It's configurable to choose any response type you like, you just have to handle parsing the network response you receive back. The library comes built in with three standard response types:
- StringListener - returns a string
- JsonElementListener - returns a
com.google.gson.JsonElement - NetworkResponseListener - returns a
com.volley.NetworkResponse
Thanks to the builder paradigm, you can also configure optional parameters for every request.
publicclassSampleConfigureActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){// boilerplate...Projectile.draw(this).aim("http://www.myapiurl/users") .method(Method.POST) .addParam("user", "nick") .addParam("password", "mypassword") .timeout(5000) .priority(Priority.MEDIUM) .fire(newStringListener(){@OverridepublicvoidonResponse(Stringresponse){Log.d(Sample.class.getName(), response)} @OverridepublicvoidonError(VolleyErrorerror){Log.d(Sample.class.getName(), "Error")} })} }| Method | Description |
|---|---|
method(Method method) | http method to be used for the request |
priority(Priority priority) | priority for volley to set on the request |
addHeader(String key, String value) | add header value to request |
addHeaders(Map<String, String> headers) | add map of header values to request |
addParam(String key, String value) | add request parameter (type depends on HTTP method) |
addParams(Map<String, String> params) | add map of parameters to request (type depends on HTTP method) |
retryPolicy(RetryPolicy policy) | retry policy to use for the request; if set, priority, timeout, and retryCount configureables are ignored |
timeout(int timeout) | time in milliseconds for a request to timeout |
retryCount(int count) | number of times to retry the request |
backoffMultiplier(float multiplier) | backoff multiplier to apply to socket timeout per retry |
tag(Object tag) | tag to set on the request for use in cancelling |
shouldCache(boolean cacheRequest) | determines whether or not the request should be cached |
Maven:
<dependency> <groupId>org.nicktate</groupId> <artifactId>projectile</artifactId> <version>(insert latest version)</version> </dependency>or Gradle:
compile 'org.nicktate:projectile:(insert latest version)'