22

Apache HTTP クライアントを使用して HTTPPost 呼び出しを行ってから、Jackson を使用して応答からオブジェクトを作成しようとしています。これが私のコードです:

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 回実行されるループで呼び出されます。基本的に、次の行のコメントを外すと問題が発生します。

//  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.

それ以外の場合は正常に動作します。誰かが私が間違っていることを教えてください。

4

6 に答える 6

2

私も同じ問題を抱えていました。

postResponse を使用する場合は、別の場所で再度使用できるように変数に入れる必要があります。それ以外の場合、接続は閉じられ、同じ応答を再度使用することはできなくなります。

以前は(デバッグ目的で)ログに記録していましたが、常に失敗します。

于 2015-09-22T13:11:17.397 に答える