Apache HTTP İstemcisi'ni kullanarak bir HTTPPost çağrısı yapıyorum ve daha sonra Jackson kullanarak yanıttan bir nesne oluşturmaya çalışıyorum.java.io.IOException: Kapalı akıştan okumaya teşebbüs
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");
}
Yöntem makePublisherApiCall() 100 kez söylemek için çalışan bir döngü içinde adı verilecek: İşte benim kodudur. Ben özel durum alıyorum uncommenting sonra
// Response<PublisherReportResponse> response = mapper.readValue(postResponse.getEntity().getContent(), Response.class);
// log.info("Reponse "+response.toString());
:
Attempted read from closed stream.
17:26:59,384 ERROR com.inmobi.reporting.automation.reportingmanager.ReportingAPICall - java.io.IOException: Attempted read from closed stream.
Aksi takdirde gayet iyi çalışıyor ben satırın başındaki zaman Temelde sorun oluşur. Birisi lütfen yanlış yaptığımı bana bildirin.
Ama bu 2 satırın sorun oluşturduğunu düşündüm. // Response response = mapper.readValue (postResponse.getEntity(). GetContent(), Response.class); // log.info ("Yanıt" + response.toString()); Yorum yaptıktan sonra bu program iyi çalışıyor. –
Pratik
Evet tabi. Eğer haklıysam sorun şu ki, sadece ** bir kez varlığın içeriğini tüketebilirsin. Günlüğe kaydetme ve ardından ayrıştırmadan iki kez yaptığınızdan şüpheleniyorum. Şüphemimin doğru olup olmadığını kontrol etmek isterseniz, 'log.info (EntityUtils.toString (postResponse.getEntity())) 'i yorumlayın ve aşağıdaki iki satırı kaldırın, ardından çalışıyorsa tekrar kontrol edin. – Pyranja
Teşekkürler Pyranja. Bu problemi çözdü – Pratik