java.io.IOException:尝试从关闭的流中读取 [英] java.io.IOException: Attempted read from closed stream

查看:47
本文介绍了java.io.IOException:尝试从关闭的流中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache HTTP 客户端进行 HTTPPost 调用,然后我尝试使用 Jackson 从响应中创建一个对象.这是我的代码:

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

方法 makePublisherApiCall() 将在一个循环中被调用,例如 100 次.当我取消注释该行时,基本上会出现问题:

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

取消注释后出现异常:

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.

推荐答案

EntityUtils.toString(postResponse.getEntity()) 对响应实体做了什么?我怀疑它正在消耗实体的内容流.HttpClient javadoc 指出,只有可重复的实体才能被多次使用.因此,如果实体不可重复,则您无法再次将内容流提供给映射器.为避免这种情况,您应该只让映射器使用流 - 如果需要记录内容,请记录解析的 Response 对象.

What does EntityUtils.toString(postResponse.getEntity()) do with the response entity? I would suspect, that it is consuming the entity's content stream. The HttpClient javadoc states, that only entities, which are repeatable can be consumed more than once. Therefore if the entity is not repeatable you cannot feed the content stream to the mapper again. To avoid this you should only let the mapper consume the stream - if logging of content is required, log the parsed Response object.

这篇关于java.io.IOException:尝试从关闭的流中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆