Better connection + some cleanup in RealDatabaseConnector

This commit is contained in:
Przemek Grondek 2014-07-22 12:32:15 +02:00
parent ddcb71b475
commit 72edd5f40a

View file

@ -45,14 +45,23 @@ public class RealDatabaseConnector implements DatabaseConnector {
private static String httpRequest(String urlRequest) throws IOException { private static String httpRequest(String urlRequest) throws IOException {
disableStrictMode(); // FIXME disableStrictMode(); // FIXME
URL url = new URL(urlRequest); URL url = new URL(urlRequest);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // TODO Handling no connection
InputStream inputStream = null; InputStream inputStream = null;
String response; String response;
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // TODO Handling no connection
urlConnection.setReadTimeout(10000 /* miliseconds FIXME*/);
urlConnection.setConnectTimeout(15000 /* miliseconds FIXME */);
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true); // TODO what it does?
urlConnection.connect();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try { try {
inputStream = new BufferedInputStream(urlConnection.getInputStream()); inputStream = urlConnection.getInputStream();
response = readStream(inputStream); response = readStream(inputStream);
} finally { } finally {
if(inputStream!=null)
inputStream.close();
urlConnection.disconnect(); urlConnection.disconnect();
} }
return response; return response;
@ -63,7 +72,7 @@ public class RealDatabaseConnector implements DatabaseConnector {
} }
} }
private static String readStream(InputStream in) { private static String readStream(InputStream in) throws IOException {
String streamOutput = ""; String streamOutput = "";
BufferedReader reader = null; BufferedReader reader = null;
try { try {
@ -72,15 +81,9 @@ public class RealDatabaseConnector implements DatabaseConnector {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
streamOutput += line; streamOutput += line;
} }
} catch (IOException e) {
e.printStackTrace();
} finally { } finally {
if (reader != null) { if (reader != null) {
try { reader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
return streamOutput; return streamOutput;