1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-23 07:46:22 +00:00

Fix some HTTP client errors, especially closing write output

This fixes issue #162
This commit is contained in:
Loic Blot 2017-11-08 23:39:18 +01:00
parent 37c8f29f1e
commit fe2a27f4cb
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987

View File

@ -113,7 +113,7 @@ public class OCHttpClient {
} }
private Pair<Integer, JSONObject> post(String oc_call, String data) throws OCSyncException { private Pair<Integer, JSONObject> post(String oc_call, String data) throws OCSyncException {
Log.i(OCHttpClient.TAG, "Perform GET " + _url + oc_call); Log.i(OCHttpClient.TAG, "Perform POST " + _url + oc_call);
try { try {
return execute("POST", return execute("POST",
new URL(_url.toString() + oc_call), data, false); new URL(_url.toString() + oc_call), data, false);
@ -166,6 +166,7 @@ public class OCHttpClient {
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(requestBody.getBytes(Charset.forName("UTF-8"))); out.write(requestBody.getBytes(Charset.forName("UTF-8")));
out.close();
response = handleHTTPResponse(urlConnection, skipError); response = handleHTTPResponse(urlConnection, skipError);
} catch (IOException e) { } catch (IOException e) {
@ -180,10 +181,8 @@ public class OCHttpClient {
} }
private Pair<Integer, JSONObject> handleHTTPResponse(HttpURLConnection connection, Boolean skipError) throws OCSyncException { private Pair<Integer, JSONObject> handleHTTPResponse(HttpURLConnection connection, Boolean skipError) throws OCSyncException {
BufferedReader reader;
String response;
try { try {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
String line; String line;
@ -191,7 +190,7 @@ public class OCHttpClient {
stringBuilder.append(line).append("\n"); stringBuilder.append(line).append("\n");
} }
response = stringBuilder.toString(); String response = stringBuilder.toString();
int status = connection.getResponseCode(); int status = connection.getResponseCode();
switch (status) { switch (status) {
@ -226,6 +225,8 @@ public class OCHttpClient {
throw new OCSyncException(R.string.err_sync_http_request_returncode_unhandled, OCSyncErrorType.SERVER_ERROR); throw new OCSyncException(R.string.err_sync_http_request_returncode_unhandled, OCSyncErrorType.SERVER_ERROR);
} }
} }
reader.close();
} }
catch (IOException e) { catch (IOException e) {
throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO);