Get response body on 400 HTTP response in Android?

后端 未结 3 1474
攒了一身酷
攒了一身酷 2021-01-28 16:00

I\'m communicating with an API that I can not change that sends a 400 response when a request is not validated on the API side. It is a valid HTTP request, but the request data

3条回答
  •  旧巷少年郎
    2021-01-28 16:13

    Something like this, using org.apache.http.util.EntityUtils:

    HttpRequestBase base = new HttpGet(url);
    HttpResponse response = httpClient.execute(base);
    String jsonString = EntityUtils.toString(response.getEntity());
    

    PS: This is blind coding as I don't know what you tried yet.

    To get status code:

    int statusCode = response.getStatusLine().getStatusCode();
    

    To get the entity body in byte array:

    byte[] data = EntityUtils.toByteArray(response.getEntity());
    

提交回复
热议问题