Home » Android » Android: How get the status-code of an HttpClient request

Android: How get the status-code of an HttpClient request

Posted by: admin March 11, 2020 Leave a comment

Questions:

I want to download a file and need to check the response status code (ie HTTP /1.1 200 OK).
This is a snipped of my code:

HttpGet httpRequest = new HttpGet(myUri);
HttpEntity httpEntity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
...

How do i get the status-code of the response?

How to&Answers:

This will return the int value:

response.getStatusLine().getStatusCode()

Answer:

if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_OK){
...
}