From 84de50f4065f78b8917e08f40ffe83257b32ab14 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 11 Apr 2018 17:18:11 +0200 Subject: [PATCH] Can retrieve informations even in case of API errors. --- .../client/data/helpers/APIRequestHelper.java | 19 +++++++++++++-- .../data/models/APIRequestParameters.java | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) 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; + } }