我正在使用Android Azure移动服务SDK连接到Azure移动服务。当我使用MobileServiceClient的invokeApi时,当Api在60秒内完成时,一切都很好。
然而,当我的这个Api的方法超过1分钟时,"onCompleted"方法中的"exception"显示"Error which processing request"。
我搜索了所有关于Android Azure移动服务SDK和类MobileServiceClient的文档,找不到任何与此有关的设置,大约60秒的超时设置供我配置。
有人能解释一下吗?
非常感谢。
实现您的自定义OkHttpClientFactory。例如:
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;
}
}
并将此类的实例设置为MobileServiceClient
MobileServiceClient mobileServiceClient = new MobileServiceClient(SERVICE_URL, Context);
mobileServiceClient.setAndroidHttpClientFactory(new MyOkHttpClientFactory());
这对我有用
OkHttpClient的默认超时值为10秒。
By default azure is using AndroidHttpClient. This has a default socket operation timeout 1 min(SOCKET_OPERATION_TIMEOUT = 60 * 1000)
我们可以简单地将其编辑为
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();
}
}
这将解决问题。
经过长期研究,Azure Mobile Service for Android使用MobileServiceClient类,并使用AndroidHttpClient与我们的Azure Mobile Services url进行SSL通信。而AndroidHttpClient,从这个后android httpclient挂起第二个到服务器的请求(连接超时),有一个默认的"合理"设置,即SSL访问超时60秒!OMG.
不知道Azure Mobile Service for Android团队中的人是否能看到这篇文章,并为我们找到一种方法,使超时设置直接在Azure Mobile Service of Android类中进行配置。