Search in sources :

Example 1 with DefaultResponseStatusHandler

use of com.dg.libs.rest.handlers.DefaultResponseStatusHandler 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();
}
Also used : Response(com.squareup.okhttp.Response) DefaultResponseStatusHandler(com.dg.libs.rest.handlers.DefaultResponseStatusHandler) OkHttpClient(com.squareup.okhttp.OkHttpClient) ResponseStatus(com.dg.libs.rest.domain.ResponseStatus) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) HttpRequest(com.dg.libs.rest.HttpRequest) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HttpRequest (com.dg.libs.rest.HttpRequest)1 ResponseStatus (com.dg.libs.rest.domain.ResponseStatus)1 DefaultResponseStatusHandler (com.dg.libs.rest.handlers.DefaultResponseStatusHandler)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1