IllegalStateException:已消耗内容 - 如何解决? [英] IllegalStateException: Content has been consumed - How to resolve?
本文介绍了IllegalStateException:已消耗内容 - 如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
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对象。所以我第二次得到例外。但是虽然我知道这个问题,但我找不到一个简单的解决方案。那么解决这个问题的好方法是什么?
The problem here is that I read two times the HttpResponse object. So the second time I get the exception. But although I know the problem, I can´t find an easy solution. So what is a good way to solve that problem?
推荐答案
读取响应返回的输入流。将getEntity()。getContent()
转换为 byte []
,存储在局部变量中。请参阅将InputStream转换为Java中的字节数组。
Read the input stream returned by response.getEntity().getContent()
into a byte[]
, stored in a local variable. See 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");
这篇关于IllegalStateException:已消耗内容 - 如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文