Sam*_*awy 39 android http rest-client
通常我习惯使用类来检查我的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;
}
Run Code Online (Sandbox Code Playgroud)
但是根据官方文档,最近该类因API级别22而被弃用
此接口在API级别22中已弃用.请改用openConnection().请访问此网页了解更多详情.
但OpenConnection()对我来说使用方法没有任何意义.
我的问题是:有没有其他方法可以执行相同的功能而无需在应用程序中自行对所有代码响应进行硬编码?
提前致谢.
Bla*_*elt 84
您可以将返回值强制转换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) {
}
Run Code Online (Sandbox Code Playgroud)
HttpURLConnection.getResponseCode()文档在这里
| 归档时间: |
|
| 查看次数: |
16588 次 |
| 最近记录: |