diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-12 16:01:13 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-12 18:21:16 +0200 |
commit | 5e72c5c53228952846072b64fa70d9a13dc334d6 (patch) | |
tree | da36ea4fd39a19e5b460924bbb156b74ca58ffaf /main/src | |
parent | e1f0ec8c89d187117bb8c8dcaf7192a5056c7b4a (diff) | |
download | cgeo-5e72c5c53228952846072b64fa70d9a13dc334d6.zip cgeo-5e72c5c53228952846072b64fa70d9a13dc334d6.tar.gz cgeo-5e72c5c53228952846072b64fa70d9a13dc334d6.tar.bz2 |
Add the possibility to set headers to a GET request
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/network/Network.java | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index bd3c254..8d06cd5 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -192,7 +192,9 @@ public abstract class Network { * GET HTTP request * * @param uri + * the URI to request * @param params + * the parameters to add the the GET request * @param my * @param addF * @return @@ -205,41 +207,65 @@ public abstract class Network { * GET HTTP request * * @param uri + * the URI to request * @param params - * @param cacheFile - * the name of the file storing the cached resource, or null not to use one - * @return + * the parameters to add the the GET request + * @param headers + * the headers to add to the GET request + * @return the HTTP response */ - public static HttpResponse request(final String uri, final Parameters params, final File cacheFile) { + public static HttpResponse request(final String uri, final Parameters params, final Parameters headers) { final String fullUri = params == null ? uri : Uri.parse(uri).buildUpon().encodedQuery(params.toString()).build().toString(); final HttpRequestBase request = new HttpGet(fullUri); request.setHeader("X-Requested-With", "XMLHttpRequest"); + if (headers != null) { + for (final NameValuePair header : headers) { + request.setHeader(header.getName(), header.getValue()); + } + } + return Network.request(request); + } + + /** + * GET HTTP request + * + * @param uri + * the URI to request + * @param params + * the parameters to add the the GET request + * @param cacheFile + * the name of the file storing the cached resource, or null not to use one + * @return the HTTP response + */ + public static HttpResponse request(final String uri, final Parameters params, final File cacheFile) { if (cacheFile != null && cacheFile.exists()) { final String etag = LocalStorage.getSavedHeader(cacheFile, "etag"); if (etag != null) { - request.setHeader("If-None-Match", etag); + return request(uri, params, new Parameters("If-None-Match", etag)); } else { final String lastModified = LocalStorage.getSavedHeader(cacheFile, "last-modified"); if (lastModified != null) { - request.setHeader("If-Modified-Since", lastModified); + return request(uri, params, new Parameters("If-Modified-Since", lastModified)); } } } - return Network.request(request); + return request(uri, params, (Parameters) null); } /** * GET HTTP request * * @param uri + * the URI to request * @param params - * @return + * the parameters to add the the GET request + * @return the HTTP response */ public static HttpResponse request(final String uri, final Parameters params) { - return request(uri, params, null); + return request(uri, params, (Parameters) null); } /** @@ -250,7 +276,7 @@ public abstract class Network { * @return the HTTP response */ public static HttpResponse request(final String uri) { - return request(uri, null, null); + return request(uri, null, (Parameters) null); } public static HttpResponse request(final HttpRequestBase request) { |