Can retrieve informations even in case of API errors.

This commit is contained in:
Pierre 2018-04-11 17:18:11 +02:00
parent 39135ef637
commit 84de50f406
2 changed files with 40 additions and 2 deletions

View File

@ -54,6 +54,7 @@ public class APIRequestHelper {
APIResponse result = new APIResponse(); APIResponse result = new APIResponse();
InputStream is = null; InputStream is = null;
HttpURLConnection conn = null;
try { try {
@ -61,7 +62,7 @@ public class APIRequestHelper {
URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri()); URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri());
//The request is being performed on an http server //The request is being performed on an http server
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn = (HttpURLConnection) url.openConnection();
//Configure the connection //Configure the connection
conn.setReadTimeout(3000); conn.setReadTimeout(3000);
@ -92,6 +93,20 @@ public class APIRequestHelper {
conn.disconnect(); 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 { } finally {
//Close streams //Close streams
if(is != null) if(is != null)

View File

@ -28,6 +28,11 @@ public class APIRequestParameters {
*/ */
private String request_uri; private String request_uri;
/**
* Set if the connection should be parsed even in case of error
*/
private boolean tryContinueOnError = false;
/** /**
* The class constructor * The class constructor
* *
@ -115,4 +120,22 @@ public class APIRequestParameters {
public Context getContext() { public Context getContext() {
return context; 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;
}
} }