11

I've managed to make changes to everything but the following:

HttpClient client;
HttpPost method;   
client = new DefaultHttpClient();
method = new HttpPost(url); 

InputStream rstream;
try {
    rstream = method.getResponseBodyAsStream();
} catch (IOException e) {
    return BadSpot(e.getMessage()); 
}

What I'm not sure of is what I should replace getResponseBodyAsStream() with.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jenny
  • 251
  • 2
  • 7
  • 14

4 Answers4

5
InputStream rstream;
try {
    HttpResponse response = client.execute(HttpHost, method);
    rstream = response.getEntity().getContent();
} catch (IOException e) {
    return BadSpot(e.getMessage()); 
}

above should do what you are asking.

javanna
  • 59,145
  • 14
  • 144
  • 125
fmucar
  • 14,361
  • 2
  • 45
  • 50
2

The util class has some helpful methods:

EntityUtils.toString(response.getEntity());

There is also some useful stuff in the examples at apache's website

Jess
  • 23,901
  • 21
  • 124
  • 145
Nate
  • 21
  • 1
2

HttpResponse.getEntity(), followed by HttpEntity.getContent()

Anon
  • 2,654
  • 16
  • 10
0

Use EntityUtils and check the returned entity to be not null before consuming the entity:

InputStream rstream;
try {
    HttpResponse response = client.execute(HttpHost, method);

    rstream = Optional.ofNullable(httpResponse.getEntity())
    .map(e -> response.getContent()).orElse(null);

} catch (IOException e) {
    return BadSpot(e.getMessage()); 
}

NOTE: the InputStream here can be null and most of all you have to ensure that it's consumed before you actually close the response/release the connection.

IvanR
  • 198
  • 1
  • 1
  • 12