java.io.IOException: Attempted read from closed stream

2013-09-05T20:14:39

I am making a HTTPPost call using Apache HTTP Client and then I am trying to create an object from the response using Jackson. Here is my code:

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

Method makePublisherApiCall() will be called in a loop which runs for say 100 times. Basically problem occurs when I uncomment the line:

//  Response<PublisherReportResponse> response = mapper.readValue(postResponse.getEntity().getContent(), Response.class);
//  log.info("Reponse "+response.toString());

After uncommenting I am getting exception:

Attempted read from closed stream.
17:26:59,384 ERROR com.inmobi.reporting.automation.reportingmanager.ReportingAPICall - java.io.IOException: Attempted read from closed stream.

Otherwise it works fine. Could someone please let me know what I am doing wrong.

Copyright License:
Author:「Pratik」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/18636046/java-io-ioexception-attempted-read-from-closed-stream

About “java.io.IOException: Attempted read from closed stream” questions

Am trying to view a wsdl file from SOAPUI but it throws the following error. I have researched the internet for a way to fix this but to no avail. What could be the issue here? Error loading [http...
I am making a HTTPPost call using Apache HTTP Client and then I am trying to create an object from the response using Jackson. Here is my code: private static final Logger log = Logger.getLogger(
I am trying to parse xml, but i am getting error while setInput method parser.setInput(responseStream, null); responseStream value are getting fine. error is like this, 08-28 15:47:37.069: WARN/...
I am trying to parse xml, but i am getting error while setInput method parser.setInput(responseStream, null); responseStream value are getting fine. error is like this, 08-28 15:47:37.069: WARN/...
I have come across many blogs that answers a way to read response body like below Response response = e.getResponse(); String body; try { Reader reader = res...
Considering the code below, when i call foo1() then foo2(), everything is good. But when i call them together, i randomly get the error: Error converting result java.io.IOException: Attempted read...
I want to process a file using aws s3 bucket with java. My Code AmazonS3 s3 = AmazonS3ClientBuilder.standard().build(); // Get the object from the event and show its content type String srcBucket =
I'm trying to find a way to avoid the IOException related to the fact that I read on a closed stream. I'm calling a webservice method that returns a Stream: InputStream stream =
When I used two HTTP endpoints in a flow, Mule throws this exception. I've found the way to deal with this problem: use the second HTTP endpoint in asynchronous mode, but it is not a good way. ERROR
I am trying to parse a response from the Apache HttpClient (3.1) response like this: try { int code = mclient.executeMethod(method); if (code == Status.OK.getCode()) { st = me...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.