I am making a call to Twitter search and getting JSON response. My understanding is that if the twitter request limit is exceeded this will be reported in HttpResponse code. How can I obtain the response code from the HttpClient classes after the request is made?
Also does anyone know if this is in fact how twitter reports the rate limit exceeded?
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
// How can I get HTTP response code?
}
catch(Exception ex)
{
ex.printStackTrace();
}
Don’t use a response handler at all – you can get the status code from a plain
HttpResponseinstance usingHttpResponse#getStatusLine():Recommended reading:
HttpResponseStatusLineN.B. when
BasicResponseHandlerreturns aStringinstead of throwing an exception, that implies the response code was a2xx(that is,200 <= code < 300).