通常,我使用RestClient
类中的org.apache.http.HttpStatus
类来检查服务器的不同代码响应,如下所示:
if (HttpStatus.SC_OK == restClient.getResponseCode()) {
//200 OK (HTTP/1.0 - RFC 1945)
return true;
} else if (HttpStatus.SC_BAD_GATEWAY == restClient.getResponseCode()){
//502 Bad Gateway (HTTP/1.0 - RFC 1945)
return false;
}
但是最近这个类变成了,因为API级别为22,根据官方的文档。
这个接口在API级别22中被废弃。请改用openConnection()。详情请访问本网页。
但是对我来说,使用OpenConnection()
方法没有任何意义。
,我的问题是:是否有其他方法来完成相同的功能,而不需要自己在应用程序中对所有的代码响应进行硬编码?
提前谢谢。
发布于 2015-04-29 10:47:24
可以将openConnection
的返回值转换为HttpURLConnection
,并使用getResponseCode()
检索响应代码。
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
final int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
} else if (responseCode == HttpURLConnection.HTTP_BAD_GATEWAY) {
}
HttpURLConnection.getResponseCode()
文档是这里
https://stackoverflow.com/questions/29941328
复制相似问题