use of com.dg.libs.rest.domain.ResponseStatus in project android-rest-client by darko1002001.
the class RestClientRequest method runRequest.
protected void runRequest() {
if (handler == null) {
handler = new UIThreadResponseHandler<>(callback);
}
if (statusHandler == null) {
statusHandler = new DefaultResponseStatusHandler();
}
OkHttpClient client = generateClient();
StringBuilder url = new StringBuilder(this.url);
StringBuilder queryParams = this.queryParams;
if (queryParams != null) {
url.append(queryParams);
}
request.url(url.toString());
if (this.authenticationProvider != null) {
authenticationProvider.authenticateRequest(this);
}
Request preparedRequest = request.build();
Response response;
try {
response = client.newCall(preparedRequest).execute();
} catch (final Exception e) {
ResponseStatus responseStatus = ResponseStatus.getConnectionErrorStatus();
Log.e(TAG, responseStatus.toString(), e);
handler.handleError(responseStatus);
doAfterRunRequestInBackgroundThread();
return;
}
final ResponseStatus status = new ResponseStatus(response.code(), response.message());
Log.d(TAG, status.toString());
if (handleResponseStatus(status)) {
doAfterRunRequestInBackgroundThread();
return;
}
try {
if (parser != null) {
InputStream instream = response.body().byteStream();
final T responseData = parser.parse(instream);
close(response.body());
doAfterSuccessfulRequestInBackgroundThread(responseData);
handler.handleSuccess(responseData, status);
} else {
Log.i(TAG, "You haven't added a parser for your request");
handler.handleSuccess(null, status);
doAfterSuccessfulRequestInBackgroundThread(null);
}
} catch (final Exception e) {
ResponseStatus responseStatus = ResponseStatus.getParseErrorStatus();
Log.d(TAG, responseStatus.toString(), e);
handler.handleError(responseStatus);
}
doAfterRunRequestInBackgroundThread();
}
Aggregations