gpt4 book ai didi

java - HttpRequestRetryHandler 没有被调用

转载 作者:行者123 更新时间:2023-12-02 06:55:57 24 4
gpt4 key购买 nike

我使用 HttpClientHttpRequestRetryHandler 在连接或套接字超时时重试。因此,我关闭连接并调用 API,期望调用 HttpRequestRetryHandler。但它没有被调用。甚至没有进入方法。

代码:

HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT,
CONNECTION_TIMEOUT * 1000);
client.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
SOCKET_TIMEOUT * 1000);

// Retry handler which handles request retry
HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() {

@Override
public boolean retryRequest(IOException exception,
int executionCount, HttpContext context) {

if ((exception instanceof ConnectTimeoutException || exception instanceof SocketTimeoutException)
&& executionCount <= MAX_TRIES) {

Log.d(TAG, "Retrying connection.");
return true;

}

return false;
}
};

((AbstractHttpClient) client)
.setHttpRequestRetryHandler(requestRetryHandler);

但同时我 try catch ConnectTimeoutException ,如下所示,并且执行确实进入 ConnectTimeoutException 的 catch block 。

try {

// Making API request

} catch (ConnectTimeoutException ce) {

// Execution enters here
ce.printStackTrace();
}

我的 avd 在 Android 版本的 jelly bean 上运行。

代码有问题吗?

打印 ConnectTimeoutException 跟踪时的 Logcat:

enter image description here

最佳答案

我遇到了同样的问题,我想分享我的解决方案(对我有用)。

许多常用的 HTTP 服务器被配置为在一段时间不活动后断开持久连接,以节省系统资源,通常不会通知客户端。请参阅HttpClient Connection keep alive strategy

如果服务器断开连接,您的 HttpRequestRetryHandler 将没有机会重试(完成其工作)。

所以我是这样解决的:

  1. 实现了自定义连接保持 Activity 策略

    mHttpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
    // Honor 'keep-alive' header
    HeaderElementIterator it = new BasicHeaderElementIterator(
    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
    while (it.hasNext()) {
    HeaderElement he = it.nextElement();
    String param = he.getName();
    String value = he.getValue();
    if (value != null && param.equalsIgnoreCase("timeout")) {
    try { return Long.parseLong(value) * 1000; }
    catch(NumberFormatException ignore) { }
    }
    }

    ......

    //.Keep Alive for 30 secs
    return 30 * 1000;
    }
    });
  2. 然后我实现了自定义重试处理程序:

    HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {

    public boolean retryRequest(IOException exception,
    int executionCount,
    HttpContext context) {
    // retry a max of 5 times
    if (executionCount >= 5) return false;

    if (exception instanceof NoHttpResponseException) {
    // Retry if the server dropped connection on us
    return true;
    }

    //.TODO your custom logic...

    Boolean b = (Boolean)
    context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());
    if (!sent) {
    // Retry if the request has not been sent fully or
    // if it's OK to retry methods that have been sent
    return true;
    }

    // otherwise do not retry
    return false;
    }
    };

    mHttpClient.setHttpRequestRetryHandler(retryHandler);

关于java - HttpRequestRetryHandler 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com