1

I'm using Android Azure Mobile Services SDK to connect to Azure Mobile Services. When I use invokeApi of MobileServiceClient, everything is fine when the Api complete within 60 seconds.

However, when my method of this Api gets longer than 1 minute, the "exception" in the "onCompleted" method shows "Error while processing request".

And I search all the documents about the Android Azure Mobile Services SDK and the class MobileServiceClient, I can't find any setting regarding to this around 60 seconds timeout setting for me to configure.

Could anyone shed some light on it?

thanks alot.

Paul
  • 437
  • 2
  • 14
  • Is the timeout in the client SDK or on the server execution? I'm sure it is surfacing on the client, but where is it originating? What is your custom API doing that would make it time out - making an outbound HTTP request? – Matt Milner Sep 17 '14 at 16:16

3 Answers3

4

Implement your custom OkHttpClientFactory. For example:

public class MyOkHttpClientFactory implements OkHttpClientFactory {
    @Override
    public OkHttpClient createOkHttpClient() {
        long timeout = 30_000;
        TimeUnit unit = TimeUnit.MILLISECONDS;
        OkHttpClient okClient = new OkHttpClient();

        okClient.setConnectTimeout(timeout, unit);
        okClient.setReadTimeout(timeout, unit);
        okClient.setWriteTimeout(timeout, unit);

        return okClient;
    }
}

and set instance of this class to MobileServiceClient

MobileServiceClient mobileServiceClient = new MobileServiceClient(SERVICE_URL, Context);
mobileServiceClient.setAndroidHttpClientFactory(new MyOkHttpClientFactory());

this works for me

Default timeout values for OkHttpClient is 10 seconds.

potmesil
  • 43
  • 4
1

After a long research, it turns out that Azure Mobile Service for Android uses class MobileServiceClient, and it uses AndroidHttpClient to make the SSL communication with our Azure Mobile Services url. AND the AndroidHttpClient, from this post android httpclient hangs on second request to the server (connection timed out) , has a default "reasonable" settings which is 60 seconds timeout for SSL access! OMG.

Don't know if someone who is in the team of Azure Mobile Service for Android can see this post and figure out a way for us to make the timeout setting configurable directly in the class of Azure Mobile Service for Android.

Community
  • 1
  • 1
Paul
  • 437
  • 2
  • 14
1
    By default azure is using AndroidHttpClient. This has a default socket operation timeout 1 min(SOCKET_OPERATION_TIMEOUT = 60 * 1000)

we can simply edit this as

 HttpConnectionParams.setConnectionTimeout(client.getParams(), SOCKET_OPERATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_OPERATION_TIMEOUT);

    Inside azure sdk there is an interface ServiceFilterRequest and it holds a method 
     public ServiceFilterResponse execute() throws Exception;

    We can edit this method and add our custom interval as follows.
    1. Open its implementation class ServiceFilterRequestImpl.
    2. set a connection interval time 

        // I'm changing the default 1 min to 5 minutes..
        private static final int SOCKET_OPERATION_TIMEOUT = 5 * 60 * 1000;

    3. edit  public ServiceFilterResponse execute() throws Exception { } method

      public ServiceFilterResponse execute() throws Exception {
            // Execute request
            AndroidHttpClient client = mAndroidHttpClientFactory.createAndroidHttpClient();
            client.getParams().setParameter(HTTP.USER_AGENT, MobileServiceConnection.getUserAgent());


 //Overriding the default interval with the desired connection timeout value.
            HttpConnectionParams.setConnectionTimeout(client.getParams(), SOCKET_OPERATION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_OPERATION_TIMEOUT);



            try {
                final HttpResponse response = client.execute(mRequest);
                ServiceFilterResponse serviceFilterResponse = new ServiceFilterResponseImpl(response);
                return serviceFilterResponse;
            } finally {
                client.close();
            }
        }

This will resolve the issue.

rajeesh
  • 937
  • 10
  • 11