2010-04-07 100 views
85

我想下载一个文件并需要检查响应状态码(即HTTP /1.1 200 OK)。 这是一个剪断我的代码:Android:如何获取HttpClient请求的状态码

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

我要如何响应的状态码?

回答

209

这将返回int值:

response.getStatusLine().getStatusCode() 
+30

然后你可以用HTTP状态代码比较http://developer.android.com/reference/org/apache/http/HttpStatus.html – thanhbinh84 2011-12-27 08:30:49

+2

由于HttpStatus是一个不赞成的类,有没有更好的类来使用? – gaara87 2015-05-06 18:10:36

+0

@AkashRamani - 是的,只需使用HttpUrlConnection方法:.getResponseCode() – 2015-07-31 21:16:08

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