我在Android中使用RoboSpice for Rest Api调用,我想在调用中添加30秒的连接超时我将如何做?
这是我的代码
public class AddBrandsService extends
SpringAndroidSpiceRequest<AddBrands.Response> {
public final AddBrands.Response loadDataFromNetwork(){
return getRestTemplate().postForObject(url,
request, AddBrands.Response.class);
}
}
this service is called here
private SpiceManager contentManager = new SpiceManager(
JacksonSpringAndroidSpiceService.class);
contentManager.execute(service, lastRequestCacheKey,
DurationInMillis.ONE_SECOND, new AddBrandsListner());
提前感谢...
答案 0 :(得分:3)
这是代码。基本上,你必须在两个不同的实现之间处理android的版本作为spring android切换,以避免网络堆栈中的已知错误。不幸的是,两种实现都不共享一个与超时相关的公共接口。
private void manageTimeOuts(RestTemplate restTemplate) {
// set timeout for requests
ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
if (factory instanceof HttpComponentsClientHttpRequestFactory) {
HttpComponentsClientHttpRequestFactory advancedFactory = (HttpComponentsClientHttpRequestFactory) factory;
advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
} else if (factory instanceof SimpleClientHttpRequestFactory) {
SimpleClientHttpRequestFactory advancedFactory = (SimpleClientHttpRequestFactory) factory;
advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
}
}