Get body of Bad Request httpURLConnection.getInputStream()

后端 未结 3 526
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 20:55

I\'ve been working on a portlet that calls Rest API. When the API is called and the requested data doesn\'t exist, it returns an appropriate error message in JSON format (wi

3条回答
  •  醉话见心
    2020-12-15 21:49

    you can get body of Bad Request in HttpURLConnection using this code :

    InputStream errorstream = connection.getErrorStream();
    
    String response = "";
    
    String line;
    
    BufferedReader br = new BufferedReader(new InputStreamReader(errorstream));
    
    while ((line = br.readLine()) != null) {
        response += line;
    }
    
    Log.d("body of Bad Request HttpURLConnection", "Response: " + response);
    

提交回复
热议问题