Android: How get the status-code of an HttpClient request
AndroidHttpclientHttprequestHttp Status-CodesAndroid 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()