diff options
Diffstat (limited to 'src/io/AbstractRequester.java')
-rw-r--r-- | src/io/AbstractRequester.java | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/src/io/AbstractRequester.java b/src/io/AbstractRequester.java index 342bba9..ac74533 100644 --- a/src/io/AbstractRequester.java +++ b/src/io/AbstractRequester.java @@ -35,23 +35,27 @@ public abstract class AbstractRequester implements Requester { private JSONObject getJSON(String resource, boolean checkStatusCode) throws IOException { HttpURLConnection conn = open(buildUrl(resource)); - preconnect(conn); - JSONObject resp = getResponseAsJson(conn); - /* print response to stderr for debugging */ - if (resp.has("errors")) { - try { - String errors = resp.get("errors").toString(); - getLogger().info("Request failed: " + errors); - } catch (JSONException ex) { + try { + preconnect(conn); + JSONObject resp = getResponseAsJson(conn); + /* print response to stderr for debugging */ + if (resp.has("errors")) { + try { + String errors = resp.get("errors").toString(); + getLogger().info("Request failed: " + errors); + } catch (JSONException ex) { + } } + // TODO: what if there is an internal server error? Technically we + // should always treat that as "don't know what the server thinks". + if (checkStatusCode && conn.getResponseCode() != 200) { + // TODO: print more helpful details + throw new IOException("Unexpected response code"); + } + return resp; + } finally { + conn.disconnect(); } - // TODO: what if there is an internal server error? Technically we - // should always treat that as "don't know what the server thinks". - if (checkStatusCode && conn.getResponseCode() != 200) { - // TODO: print more helpful details - throw new IOException("Unexpected response code"); - } - return resp; } protected final URL buildUrl(String resource) throws IOException { |