HttpClient 仅响应一行 XML
我正在使用 HttpClient 获取 XML 文件,但在返回整个文档时遇到问题(它只返回 XML 文件的一行)。所以:
DefaultHttpClient c = new DefaultHttpClient();
BasicResponseHandler r = new BasicResponseHandler();
String s = null;
try
{
s = c.execute(new HttpGet("http://localhost/activity.xml"), r);
}
catch (Exception e)
{
e.printStackTrace();
}
Log.i(TAG, s);
生成的字符串始终只是
我需要做些什么来告诉 HttpClient 加载整个文件或处理换行符什么的? XML 非常简单:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<group-id type="integer">2187</group-id>
<icon type="integer">2</icon>
<name>Android</name>
<overview>android app</overview>
<permalink>codebase</permalink>
<start-page>tickets</start-page>
<status>active</status>
</project>
I'm using HttpClient to fetch an XML file and I'm having issues getting the entire document returned (it only returns one line of the XML file). So:
DefaultHttpClient c = new DefaultHttpClient();
BasicResponseHandler r = new BasicResponseHandler();
String s = null;
try
{
s = c.execute(new HttpGet("http://localhost/activity.xml"), r);
}
catch (Exception e)
{
e.printStackTrace();
}
Log.i(TAG, s);
The resulting string is always just <?xml version="1.0" encoding="UTF-8"?>
Is there something I need to do to tell HttpClient to load the entire file or process newlines or something? XML is super simple:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<group-id type="integer">2187</group-id>
<icon type="integer">2</icon>
<name>Android</name>
<overview>android app</overview>
<permalink>codebase</permalink>
<start-page>tickets</start-page>
<status>active</status>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您传递
BasicResponseHandler
作为响应处理程序,则execute
的结果应该是整个响应正文。如果您只收到一行,我的理论是这就是服务器发送的全部内容。
查看服务器日志以查看在生成响应正文时是否引发异常。 (例如,如果您使用 JSP 进行渲染,则抛出的异常无法通过 HTTP 状态代码向客户端报告。相反,客户端将看到带有截断正文的 200 响应。)
If you pass a
BasicResponseHandler
as the response handler, the result ofexecute
should be the entire response body.If you are getting just one line, my theory is that that is all that the server is sending.
Take a look at the server logs to see if an exception is being thrown while it is generating the response body. (If you are using a JSP to do the rendering for example, an exception thrown cannot be reported back to the client via the HTTP status code. Instead the client will see a 200 response with a truncated body.)
试试这个。
如果你只想获取 xml 数据,你最好扩展 DefaultHandler。并使用像 SAXParser 、XMLReader 这样的解析器。
Try this one.
And if you want to get just xml data, you'd better extend DefaultHandler. And use a parser like SAXParser ,XMLReader.