Domanda

sono riuscito a modificare tutto, ma il seguente:

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

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

Quello che non sono sicuro è quello che dovrei sostituire getResponseBodyAsStream () con.

È stato utile?

Soluzione

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

di cui sopra dovrebbe fare quello che chiedete.

Altri suggerimenti

La classe util ha alcuni metodi utili:

EntityUtils.toString(response.getEntity());

Ci sono anche alcune cose utili negli esempi a di apache sito

Usa EntityUtils e controllare l'entità tornato ad essere not null prima di consumare l'entità:

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()); 
}

Nota. l'InputStream qui può essere nullo e soprattutto si deve garantire che sia consumato prima che realmente vicino la risposta / rilasciare la connessione

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top