diff options
Diffstat (limited to 'main/src/cgeo/geocaching/network/Network.java')
| -rw-r--r-- | main/src/cgeo/geocaching/network/Network.java | 91 |
1 files changed, 76 insertions, 15 deletions
diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index 0bd845f..5c4148f 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -22,6 +22,7 @@ import ch.boye.httpclientandroidlib.client.methods.HttpGet; import ch.boye.httpclientandroidlib.client.methods.HttpPost; import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; import ch.boye.httpclientandroidlib.client.params.ClientPNames; +import ch.boye.httpclientandroidlib.entity.StringEntity; import ch.boye.httpclientandroidlib.entity.mime.MultipartEntity; import ch.boye.httpclientandroidlib.entity.mime.content.FileBody; import ch.boye.httpclientandroidlib.entity.mime.content.StringBody; @@ -34,6 +35,7 @@ import ch.boye.httpclientandroidlib.params.HttpParams; import ch.boye.httpclientandroidlib.protocol.HttpContext; import ch.boye.httpclientandroidlib.util.EntityUtils; +import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; @@ -43,11 +45,13 @@ import android.net.Uri; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.net.URLEncoder; public abstract class Network { private static final int NB_DOWNLOAD_RETRIES = 4; + /** User agent id */ private final static String PC_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"; /** Native user agent, taken from a Android 2.2 Nexus **/ @@ -58,7 +62,7 @@ public abstract class Network { private final static HttpParams clientParams = new BasicHttpParams(); static { - Network.clientParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); + Network.clientParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, CharEncoding.UTF_8); Network.clientParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); Network.clientParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000); Network.clientParams.setParameter(ClientPNames.HANDLE_REDIRECTS, true); @@ -115,7 +119,6 @@ public abstract class Network { if (contentEncoding != null) { for (final HeaderElement codec : contentEncoding.getElements()) { if (codec.getName().equalsIgnoreCase("gzip")) { - Log.d("Decompressing response"); response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } @@ -153,6 +156,27 @@ public abstract class Network { } /** + * POST HTTP request with Json POST DATA + * + * @param uri the URI to request + * @param json the json object to add to the POST request + * @return the HTTP response, or null in case of an encoding error params + */ + public static HttpResponse postJsonRequest(final String uri, final JSONObject json) { + HttpPost request = new HttpPost(uri); + request.addHeader("Content-Type", "application/json; charset=utf-8"); + if (json != null) { + try { + request.setEntity(new StringEntity(json.toString())); + } catch (UnsupportedEncodingException e) { + Log.e("postJsonRequest:JSON Entity: UnsupportedEncodingException"); + return null; + } + } + return doRepeatedRequests(request); + } + + /** * Multipart POST HTTP request * * @param uri the URI to request @@ -206,7 +230,7 @@ public abstract class Network { request = new HttpPost(uri); if (params != null) { try { - ((HttpPost) request).setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + ((HttpPost) request).setEntity(new UrlEncodedFormEntity(params, CharEncoding.UTF_8)); } catch (final UnsupportedEncodingException e) { Log.e("request", e); return null; @@ -235,11 +259,8 @@ public abstract class Network { "X-Requested-With", "XMLHttpRequest")) { request.setHeader(header.getName(), header.getValue()); } - if (Settings.getUseNativeUa()) { - request.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Network.NATIVE_USER_AGENT); - } else { - request.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Network.PC_USER_AGENT); - } + request.getParams().setParameter(CoreProtocolPNames.USER_AGENT, + Settings.getUseNativeUa() ? Network.NATIVE_USER_AGENT : Network.PC_USER_AGENT); } /** @@ -362,15 +383,21 @@ public abstract class Network { return response != null && response.getStatusLine().getStatusCode() == 200; } + /** + * Get the result of a GET HTTP request returning a JSON body. + * + * @param uri the base URI of the GET HTTP request + * @param params the query parameters, or <code>null</code> if there are none + * @return a JSON object if the request was successful and the body could be decoded, <code>null</code> otherwise + */ public static JSONObject requestJSON(final String uri, final Parameters params) { final HttpResponse response = request("GET", uri, params, new Parameters("Accept", "application/json, text/javascript, */*; q=0.01"), null); - if (isSuccess(response)) { + final String responseData = Network.getResponseData(response, false); + if (responseData != null) { try { - return new JSONObject(Network.getResponseData(response)); + return new JSONObject(responseData); } catch (final JSONException e) { - Log.e("Network.requestJSON", e); - } catch (final NullPointerException e) { - Log.e("Network.requestJSON", e); + Log.w("Network.requestJSON", e); } } @@ -379,7 +406,7 @@ public abstract class Network { private static String getResponseDataNoError(final HttpResponse response, boolean replaceWhitespace) { try { - String data = EntityUtils.toString(response.getEntity(), "UTF-8"); + String data = EntityUtils.toString(response.getEntity(), CharEncoding.UTF_8); return replaceWhitespace ? BaseUtils.replaceWhitespace(data) : data; } catch (Exception e) { Log.e("getResponseData", e); @@ -387,10 +414,26 @@ public abstract class Network { } } + /** + * Get the body of a HTTP response. + * + * {@link BaseUtils#replaceWhitespace(String)} will be called on the result + * + * @param response a HTTP response, which can be null + * @return the body if the response comes from a successful HTTP request, <code>null</code> otherwise + */ public static String getResponseData(final HttpResponse response) { return Network.getResponseData(response, true); } + /** + * Get the body of a HTTP response. + * + * @param response a HTTP response, which can be null + * @param replaceWhitespace <code>true</code> if {@link BaseUtils#replaceWhitespace(String)} + * should be called on the body + * @return the body if the response comes from a successful HTTP request, <code>null</code> otherwise + */ public static String getResponseData(final HttpResponse response, boolean replaceWhitespace) { if (!isSuccess(response)) { return null; @@ -399,7 +442,25 @@ public abstract class Network { } public static String rfc3986URLEncode(String text) { - return StringUtils.replace(URLEncoder.encode(text).replace("+", "%20"), "%7E", "~"); + return StringUtils.replace(Network.encode(text).replace("+", "%20"), "%7E", "~"); + } + + public static String decode(final String text) { + try { + return URLDecoder.decode(text, CharEncoding.UTF_8); + } catch (UnsupportedEncodingException e) { + Log.e("Network.decode", e); + } + return null; + } + + public static String encode(final String text) { + try { + return URLEncoder.encode(text, CharEncoding.UTF_8); + } catch (UnsupportedEncodingException e) { + Log.e("Network.encode", e); + } + return null; } } |
