diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2013-01-13 11:35:40 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2013-01-13 11:38:35 +0100 |
| commit | 58e57c62fc1ef88ff190f71dfcbc79a3233d915e (patch) | |
| tree | 7e6dbf6d78f8ad5778bfee651851decffd134961 | |
| parent | 7bdfba7a8ddad0151a8883800f2906f9f922b04a (diff) | |
| download | cgeo-58e57c62fc1ef88ff190f71dfcbc79a3233d915e.zip cgeo-58e57c62fc1ef88ff190f71dfcbc79a3233d915e.tar.gz cgeo-58e57c62fc1ef88ff190f71dfcbc79a3233d915e.tar.bz2 | |
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.
| -rw-r--r-- | main/src/cgeo/geocaching/network/Network.java | 16 |
1 files 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 <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); } } |
