0

I am using olingo client android 4.0.0 library to communicate with my backend which has implemented OData protocol from android client. I want to set request timeout to my olingo request. Also I want to disable retry on connection failure.

ODataClient oDataClient = ODataClientFactory.getV4();
ODataEntityCreateRequest<ODataEntity> req = oDataClient.getCUDRequestFactory()
                            .getEntityCreateRequest(uri, oDataEntity);

I want to add timeout and disable connection retry for it.

Chetan Ashtivkar
  • 194
  • 2
  • 15

1 Answers1

1

After going through the code of Olingo client library came across a workaround for this.

ODataClient has a Configuration property, which has HttpClientFactory parameter. I had to create a class which extended DefaultHttpClientFactory and override it's methods to update HttpClient with timeout and retry policy changes.

Complete code is as below.

private class RequestRetryHttpClientFactory extends DefaultHttpClientFactory {
        private final int HTTP_REQUEST_TIMEOUT = 2 * 60 * 1000; 

        @Override
        public org.apache.http.impl.client.DefaultHttpClient create(HttpMethod method, URI uri) {
            final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
                    Log.d(getClass().getSimpleName(), "RETRY REQUEST");
                    return false;
                }
            };
            final DefaultHttpClient httpClient = super.create(method, uri);
            HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), HTTP_REQUEST_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpClient.getParams(), HTTP_REQUEST_TIMEOUT);

            httpClient.setHttpRequestRetryHandler(myRetryHandler);
            return httpClient;
        }
    }

I added this to my oDataClient as bellow.

ODataClient oDataClient = ODataClientFactory.getV4();
oDataClient.getConfiguration().setHttpClientFactory(new RequestRetryHttpClientFactory());
ODataEntityCreateRequest<ODataEntity> req = oDataClient.getCUDRequestFactory()
                            .getEntityCreateRequest(uri, oDataEntity);

I hope this will help someone.

Chetan Ashtivkar
  • 194
  • 2
  • 15