Ben
Ben

Reputation: 1837

httpclient.client.HttpResponseException: Too Many Requests

I created an app that reads data from Google Books API and display it in a recyclerView.

Sometimes the items in the recyclerView wont show.

In my log I get the following (it is not an error that crashes my app but it shows it):

V/AsyncHttpRH: Progress 538 from 1 (53800%)
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)

What can cause this error?

The way I use it is:

        MyBookClient.getInstance().getBooks( discoverBooks.getBookID(), new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response != null) {
                    final MyBook books = MyBook.fromJson( response );
                }
            }
        } );

Where:

public class MyBookClient {

private static final String API_BASE_URL = "https://www.googleapis.com/books/v1/volumes/";
private AsyncHttpClient client;

public static MyBookClient instance = new MyBookClient();

public MyBookClient() {
    this.client = new AsyncHttpClient();
}

public static MyBookClient getInstance() {
    return instance;
}

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

    }
}

}

Thank you!

Upvotes: 0

Views: 2935

Answers (1)

Kaneda
Kaneda

Reputation: 721

Probably your app is making a lot of request in a short time period, so the best strategy to get rid of the error 429 is implement a strategy of control it:

  1. you can use debouncing or throttling (will depend what fit your situation);
  2. implementing a queue of pendent requests to deliver in a delayed time to avoid the 429 error.
  3. mapping in your app when you REALLY need to make this request, so create a job to verify when the criteria is been contemplated.

In all cases you can do in a asynchronous way, delivering the a job, or an event subscribe implementation to inject the books data on the page or something.

Upvotes: 0

Related Questions