Sto effettuando una chiamata HTTPPost utilizzando Apache HTTP Client e quindi sto cercando di creare un oggetto dalla risposta utilizzando Jackson. Ecco il mio codice:java.io.IOException: tentativo di lettura dal flusso chiuso
private static final Logger log = Logger.getLogger(ReportingAPICall.class);
ObjectMapper mapper = new ObjectMapper();
public void makePublisherApiCall(String jsonRequest)
{
String url = ReaderUtility.readPropertyFile().getProperty("hosturl");
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpPost postRequest = new HttpPost(url);
StringEntity entity = new StringEntity(jsonRequest);
postRequest.addHeader("content-type", "application/json");
log.info("pub id :"+ExcelReader.publisherId);
postRequest.addHeader("accountId", ExcelReader.publisherId);
postRequest.setEntity(entity);
HttpResponse postResponse = client.execute(postRequest);
log.info(EntityUtils.toString(postResponse.getEntity()));
// Response<PublisherReportResponse> response = mapper.readValue(postResponse.getEntity().getContent(), Response.class);
// log.info("Reponse "+response.toString());
} catch (UnsupportedEncodingException ex) {
log.error(ex.getMessage());
log.error(ex);
Assert.assertTrue(false, "Exception : UnsupportedEncodingException");
} catch (ClientProtocolException ex) {
log.error(ex.getMessage());
log.error(ex);
Assert.assertTrue(false, "Exception : ClientProtocolException");
} catch (IOException ex) {
log.error(ex.getMessage());
log.error(ex);
Assert.assertTrue(false, "Exception : IOException");
}
Metodo makePublisherApiCall() sarà chiamato in un ciclo che corre per dire 100 volte. Fondamentalmente problema si verifica quando il commento dalla riga:
// Response<PublisherReportResponse> response = mapper.readValue(postResponse.getEntity().getContent(), Response.class);
// log.info("Reponse "+response.toString());
Dopo decommentando io sono sempre un'eccezione:
Attempted read from closed stream.
17:26:59,384 ERROR com.inmobi.reporting.automation.reportingmanager.ReportingAPICall - java.io.IOException: Attempted read from closed stream.
Altrimenti funziona benissimo. Qualcuno potrebbe farmi sapere cosa sto sbagliando.
Ma ho pensato che questo 2 linee causano problemi. // Response response = mapper.readValue (postResponse.getEntity(). GetContent(), Response.class); // log.info ("Reponse" + response.toString()); Come dopo aver commentato questi programmi funziona bene. –
Pratik
Sì, certo. Se ho ragione, il problema è che puoi solo ** una volta ** consumare il contenuto dell'entità. Sospetto che lo stiate facendo due volte attraverso la registrazione e poi l'analisi. Se si desidera verificare se il mio sospetto è corretto, commentare 'log.info (EntityUtils.toString (postResponse.getEntity()));' e decommentare le seguenti due righe, quindi controllare di nuovo se funziona. – Pyranja
Grazie Pyranja. Questo ha risolto il problema – Pratik