Tôi đang thực hiện cuộc gọi HTTPPost bằng cách sử dụng Apache HTTP Client và sau đó tôi đang cố gắng tạo một đối tượng từ phản hồi bằng cách sử dụng Jackson. Đây là mã của tôi:java.io.IOException: Đã đọc thử từ luồng đã đóng
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");
}
Phương pháp makePublisherApiCall() sẽ được gọi trong một vòng lặp chạy cho nói 100 lần. Về cơ bản vấn đề xảy ra khi tôi bỏ ghi chú dòng:
// Response<PublisherReportResponse> response = mapper.readValue(postResponse.getEntity().getContent(), Response.class);
// log.info("Reponse "+response.toString());
Sau uncommenting Tôi nhận được ngoại lệ:
Attempted read from closed stream.
17:26:59,384 ERROR com.inmobi.reporting.automation.reportingmanager.ReportingAPICall - java.io.IOException: Attempted read from closed stream.
Nếu nó hoạt động tốt. Có thể ai đó vui lòng cho tôi biết tôi đang làm gì sai.
Nhưng tôi nghĩ rằng 2 dòng này đang gây ra sự cố. // Phản hồi response = mapper.readValue (postResponse.getEntity(). GetContent(), Response.class); // log.info ("Reponse" + response.toString()); Như sau khi bình luận các chương trình này hoạt động tốt. –
Pratik
Chắc chắn rồi. Nếu tôi đúng, vấn đề là bạn chỉ có thể ** một lần ** tiêu thụ nội dung của thực thể. Tôi nghi ngờ rằng bạn đang thực hiện nó hai lần thông qua quá trình ghi nhật ký và sau đó là phân tích cú pháp. Nếu bạn muốn kiểm tra xem sự nghi ngờ của tôi có chính xác không, hãy bình luận ra 'log.info (EntityUtils.toString (postResponse.getEntity()));' và bỏ ghi chú hai dòng sau, sau đó kiểm tra lại nếu nó hoạt động. – Pyranja
Cảm ơn Pyranja. Điều đó giải quyết được vấn đề – Pratik