diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/APIRequestHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/APIRequestHelper.java index 66e7d58..7957c02 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/APIRequestHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/APIRequestHelper.java @@ -45,7 +45,7 @@ public class APIRequestHelper { * @param parameters The parameters to pass to the server * @return The result of the request */ - public APIResponse exec(APIRequestParameters parameters) throws Exception{ + public APIResponse exec(APIRequestParameters parameters) throws Exception { //Add API and login tokens addAPItokens(parameters); @@ -54,6 +54,7 @@ public class APIRequestHelper { APIResponse result = new APIResponse(); InputStream is = null; + HttpURLConnection conn = null; try { @@ -61,7 +62,7 @@ public class APIRequestHelper { URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri()); //The request is being performed on an http server - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn = (HttpURLConnection) url.openConnection(); //Configure the connection conn.setReadTimeout(3000); @@ -92,6 +93,20 @@ public class APIRequestHelper { conn.disconnect(); + } catch (Exception e){ + + e.printStackTrace(); + + //Check for response code + if(conn != null && parameters.isTryContinueOnError()){ + //Get response code + result.setResponse_code(conn.getResponseCode()); + } + + //Else we throw an exception + else + throw new Exception("An exception occurred while trying to connect to server !"); + } finally { //Close streams if(is != null) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/APIRequestParameters.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/APIRequestParameters.java index 1c4a9ac..d4f3618 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/APIRequestParameters.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/APIRequestParameters.java @@ -28,6 +28,11 @@ public class APIRequestParameters { */ private String request_uri; + /** + * Set if the connection should be parsed even in case of error + */ + private boolean tryContinueOnError = false; + /** * The class constructor * @@ -115,4 +120,22 @@ public class APIRequestParameters { public Context getContext() { return context; } + + /** + * Set whether the response should be parsed even in case of error + * + * @param tryContinueOnError TRUE to continue / FALSE else + */ + public void setTryContinueOnError(boolean tryContinueOnError) { + this.tryContinueOnError = tryContinueOnError; + } + + /** + * Check whether the connection should be maintained even in case of error + * + * @return TRUE if the request should be maintained / FALSE else + */ + public boolean isTryContinueOnError() { + return tryContinueOnError; + } }