我在我的 Android 应用程序中使用以下代码来使用 RestTemplates
return getRestTemplate().postForObject(url, forgotPasswordRequest,
ForgetPassword.Response.class);
现在我如何设置请求的超时并使用超时监听器来捕获请求何时超时?
亲切的问候
最佳答案
RestTemplate restTemplate = new MyRestTemplate(10*1000);
return restTemplate.postForObject(url, forgotPasswordRequest,
ForgetPassword.Response.class);
[...]
protected class MyRestTemplate extends RestTemplate {
public MyRestTemplate(int timeout) {
if (getRequestFactory() instanceof SimpleClientHttpRequestFactory) {
Log.d("HTTP", "HttpUrlConnection is used");
((SimpleClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(timeout);
((SimpleClientHttpRequestFactory) getRequestFactory()).setReadTimeout(timeout);
} else if (getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
Log.d("HTTP", "HttpClient is used");
((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setReadTimeout(timeout);
((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(timeout);
}
}
}
关于android - Spring Android RestTemplate 设置 Timeout 和 TimeoutListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16162417/