gpt4 book ai didi

java - 非法状态异常 : Content has been consumed - How to resolve?

转载 作者:行者123 更新时间:2023-11-30 04:58:24 25 4
gpt4 key购买 nike

我有以下代码:

HttpGet httpGet = new HttpGet(serverAdress + "/rootservices");
httpGet.setHeader("Accept", "text/xml");
HttpResponse response = client.execute(httpGet, localContext);

String projectURL = XMLDocumentParser.parseDocument(response.getEntity().getContent(), "oslc_scm:scmServiceProviders", "rdf:resource");
String workItemURL = XMLDocumentParser.parseDocument(response.getEntity().getContent(), "oslc_cm:cmServiceProviders", "rdf:resource");

这里的问题是我读取了两次 HttpResponse 对象。所以第二次我得到了异常(exception)。但尽管我知道问题所在,但我找不到简单的解决方案。那么有什么好的方法可以解决这个问题呢?

最佳答案

response.getEntity().getContent() 返回的输入流读取到 byte[] 中,存储在本地变量中。请参阅Convert InputStream to byte array in Java .

byte[] content = IOUtils.toByteArray(response.getEntity().getContent());

String projectURL = XMLDocumentParser.parseDocument(new ByteArrayInputStream(content), "oslc_scm:scmServiceProviders", "rdf:resource");
String workItemURL = XMLDocumentParser.parseDocument(new ByteArrayInputStream(content), "oslc_cm:cmServiceProviders", "rdf:resource");

关于java - 非法状态异常 : Content has been consumed - How to resolve?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7745915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com