HttpStatus在API级别22中被弃用 [英] HttpStatus become deprecated in API level 22

查看:507
本文介绍了HttpStatus在API级别22中被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我习惯使用 org.apache.http.HttpStatus RestClient 类中检查服务器的不同代码响应$ c> class如下例所示:

Usually I used to check for the server different code responses in my RestClient class using the org.apache.http.HttpStatus class as the following example:

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而被强烈推荐根据官方文档

But recently the class became deprecated since API level 22 according to the official documentation


此接口在API级别
22中已弃用。请使用 openConnection()。有关详细信息,请访问此网页

This interface was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

但对我来说使用 OpenConnection()方法没有任何意义。

But using OpenConnection() method for me does not make any sense.

我的问题是:是否有其他方法可以执行相同的功能,而无需在应用程序中自行对所有代码响应进行硬编码?

My Question is: Is there any other way to do the same functionality without needing to hardcode all the code responses by myself within the application?

提前致谢。

推荐答案

你可以投出返回的值 openConnection HttpURLConnection ,并使用 getResponseCode()到检索响应代码

you can cast the returned value of openConnection to HttpURLConnection, and use getResponseCode() to retrieve response code

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
final int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {

} else if (responseCode == HttpURLConnection.HTTP_BAD_GATEWAY) {

}

HttpURLConnection.getResponseCode()文档是这里

这篇关于HttpStatus在API级别22中被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆