Back to project page NetworkFacade.
The source code is released under:
Apache License
If you think the Android project NetworkFacade listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.saguinav.networkfacade; /*from w w w .ja v a2 s . c o m*/ import java.util.HashMap; import java.util.Map; public interface HttpRequest { Method getMethod(); String getUrl(); Map<String, String> getHeaders(); HttpBody getBody(); /** * * @return The time to cache in milliseconds */ long getCachePolicy(); RetryPolicy getRetryPolicy(); String getName(); static enum Method { GET, POST, PUT, DELETE } static class Hidden { protected static class DefaultHttpRequest implements HttpRequest { protected static final long DEFAULT_CACHE_POLICY = 5*60*1000; // 5 minutes protected static final RetryPolicy DEFAULT_RETRY_POLICY = new RetryPolicy(5000, 5, 1f); // Required parameters private final Method method; private final String url; // Optional parameters protected Map<String, String> headers; protected HttpBody body; protected long cachePolicy; protected RetryPolicy retryPolicy; protected String name; // For logging purposes private DefaultHttpRequest(Method method, String url) { this.method = method; this.url = url; } protected DefaultHttpRequest(Builder builder) { this(builder.getMethod(), builder.getUrl()); this.headers = builder.getHeaders(); this.body = builder.getBody(); this.cachePolicy = builder.getCachePolicy(); this.retryPolicy = builder.getRetryPolicy(); this.name = builder.getName(); } @Override public Method getMethod() { return method; } @Override public String getUrl() { return url; } @Override public Map<String, String> getHeaders() { return headers; } @Override public HttpBody getBody() { return body; } @Override public long getCachePolicy() { return cachePolicy; } @Override public RetryPolicy getRetryPolicy() { return retryPolicy; } @Override public String getName() { return name; } } } public static class Builder extends Hidden.DefaultHttpRequest { public Builder(Method method, String url) { super(method, url); this.headers = new HashMap<String, String>(); if (method == Method.GET) { this.cachePolicy = DEFAULT_CACHE_POLICY; this.retryPolicy = DEFAULT_RETRY_POLICY; } } public Builder header(String key, String value) { this.headers.put(key, value); return this; } public Builder headers(Map<String, String> headers) { this.headers.putAll(headers); return this; } public Builder body(HttpBody body) { this.body = body; return this; } public Builder cachePolicy(long cachePolicy) { this.cachePolicy = cachePolicy; return this; } public Builder retryPolicy(RetryPolicy retryPolicy) { this.retryPolicy = retryPolicy; return this; } public Builder name(String name) { this.name = name; return this; } public HttpRequest build() { return new Hidden.DefaultHttpRequest(this); } } }