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

AndroidHttpclientHttprequestHttp Status-Codes

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

This will return the int value:

response.getStatusLine().getStatusCode()

Solution 2 - Android

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

Solution 3 - Android

Use code() function of response to get its HTTP code:

val code = response.code()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhlkView Question on Stackoverflow
Solution 1 - AndroidRobby PondView Answer on Stackoverflow
Solution 2 - AndroidNickUnuchekView Answer on Stackoverflow
Solution 3 - AndroidHandyPawanView Answer on Stackoverflow