mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-12-27 22:18:58 +00:00
Can retrieve informations even in case of API errors.
This commit is contained in:
parent
39135ef637
commit
84de50f406
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user