HttpStatus become deprecated in API level 22

后端 未结 1 426

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

1条回答
  •  长发绾君心
    2020-12-25 11:05

    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() documentation is here

    0 讨论(0)
提交回复
热议问题