Android: 如何获得 HttpClient 请求的状态代码

我想下载一个文件,并需要检查响应状态代码(即 HTTP /1.1 200 OK)。 这是我的代码片段:

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

如何获得响应的状态代码?

82257 次浏览

This will return the int value:

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

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

val code = response.code()