简体   繁体   English

Android DefaultHttpClient连接重用

[英]Android DefaultHttpClient connection reuse

I am using DefaultHttpClient for connection. 我正在使用DefaultHttpClient进行连接。 What I would like to do is "keep checking if it's alive" and when timeed out, reconnect. 我想做的是“继续检查它是否还活着”,并在超时后重新连接。 How can I do that ? 我怎样才能做到这一点 ? I tried that entity is not null the consumeContent. 我试过那个实体不为null Is it not working for me. 它对我不起作用吗?

One way to handle this is to set a HttpRequestRetryHandler and deal with the retry accordingly. 处理此问题的一种方法是设置HttpRequestRetryHandler并相应地处理重试。

client.setHttpRequestRetryHandler(retryHandler);

You can also adjust the timeout via the HttpParams as noted here which may suffice depending on your exact needs; 您还可以按照此处所述通过HttpParams调整超时,这可能足以满足您的实际需求; however building in retry behavior is a good approach either way. 但是,无论哪种方式,建立重试行为都是一种不错的方法。

final HttpParams httpParameters = yourHttpClient.getParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM