我使用Android Azure移动服务SDK连接到Azure移动服务。 当我使用MobileServiceClient的invokeApi时,当Api在60秒内完成时,一切都很好。
然而,当我的这个Api的方法超过1分钟时,"异常"在" onCompleted"方法在处理请求时显示"错误"。
我搜索了有关Android Azure移动服务SDK和类MobileServiceClient的所有文档,我无法找到有关此约60秒超时设置的任何设置供我配置。
有人能说清楚吗?
非常感谢。
答案 0 :(得分:4)
实现您的自定义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秒。
答案 1 :(得分:1)
经过长时间的研究后,发现适用于Android的Azure移动服务使用了类MobileServiceClient,它使用AndroidHttpClient与我们的Azure移动服务网址进行SSL通信。 并且来自此帖子android httpclient hangs on second request to the server (connection timed out)的AndroidHttpClient具有默认的“合理”设置,即SSL访问超时60秒! OMG。
不知道Azure移动服务Android团队中的某个人是否可以看到这篇文章,并找到一种方法让我们可以直接在Azure Mobile Service for Android类中配置超时设置。
答案 2 :(得分:1)
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();
}
}
这将解决问题。