From 58e57c62fc1ef88ff190f71dfcbc79a3233d915e Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sun, 13 Jan 2013 11:35:40 +0100 Subject: Check the HTTP response data for nullness Although it should not happen, it looks like the response data from a successful HTTP request may be incorrect (for example because the connection was dropped before the whole body was received). Also, not being able to get the result is not necessarily an error. We will log it as a warning, and let the caller determine how it should be handled. Note that we removed the whitespace substitution in the process. The JSON body should already be well formed, and does not need any massaging before being used. --- main/src/cgeo/geocaching/network/Network.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index 7058bfc..5c4148f 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -383,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 null if there are none + * @return a JSON object if the request was successful and the body could be decoded, null 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); } } -- cgit v1.1