java—在asynchttprespons中使用backoff避免“请求过多”错误

gijlo24d  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(281)

我有一个从googlebooksapi读取数据的应用程序。
我用 AsyncHttpRequest 获取数据。
我开始出现以下错误:

2021-04-22 20:38:18.083 24736-24736/com.xx.yy V/AsyncHttpRH: Progress 538 from 1 (53800%)
2021-04-22 20:38:18.083 24736-24736/com.xx.yy I/chatty: uid=10089(com.xx.yy) identical 1 line
2021-04-22 20:38:18.086 24736-24736/com.xx.yy V/AsyncHttpRH: Progress 538 from 1 (53800%)
2021-04-22 20:38:18.088 24736-24736/com.xx.yy W/JsonHttpRH: onFailure(int, Header[], Throwable, JSONObject) was not overriden, but callback was received
    cz.msebera.android.httpclient.client.HttpResponseException: Too Many Requests
        at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:446)
        at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:160)
        at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
        at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
2021-04-22 20:38:18.101 24736-24736/com.xx.yy I/chatty: uid=10089(com.xx.yy) identical 5 lines
2021-04-22 20:38:18.101 24736-24736/com.xx.yy W/JsonHttpRH: onFailure(int, Header[], Throwable, JSONObject) was not overriden, but callback was received
    cz.msebera.android.httpclient.client.HttpResponseException: Too Many Requests
        at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:446)
        at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:160)
        at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
        at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
2021-04-22 20:38:18.135 24736-24736/com.xx.yy V/AsyncHttpRH: Progress 538 from 1 (53800%)
2021-04-22 20:38:18.155 24736-24736/com.xx.yy W/JsonHttpRH: onFailure(int, Header[], Throwable, JSONObject) was not overriden, but callback was received
    cz.msebera.android.httpclient.client.HttpResponseException: Too Many Requests
        at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:446)
        at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:160)
        at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
        at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

我不确定是什么原因造成的,因为我读到使用指数backoff可能会有所帮助。
我的asynchttpclient正在使用loopj库,该库具有:

BackoffManager b = new BackoffManager() {
    @Override
    public void backOff(HttpRoute route) {

    }

    @Override
    public void probe(HttpRoute route) {

    }
}

但我不知道如何在课堂上使用它:

public void getBooks(final String query, JsonHttpResponseHandler handler) {
    try {
        client.get( API_BASE_URL + URLEncoder.encode( query, "utf-8" ), handler );
    } catch (UnsupportedEncodingException ignored) {

    }
}

在我的案例中,对如何实施这一策略有什么建议吗?
谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题