HttpClient 仅响应一行 XML

发布于 2024-10-16 16:55:08 字数 914 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2024-10-23 16:55:08

如果您传递 BasicResponseHandler 作为响应处理程序,则 execute 的结果应该是整个响应正文。

如果您只收到一行,我的理论是这就是服务器发送的全部内容。

查看服务器日志以查看在生成响应正文时是否引发异常。 (例如,如果您使用 JSP 进行渲染,则抛出的异常无法通过 HTTP 状态代码向客户端报告。相反,客户端将看到带有截断正文的 200 响应。)

If you pass a BasicResponseHandler as the response handler, the result of execute 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.)

恋竹姑娘 2024-10-23 16:55:08

试试这个。

HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while((line = in.readLine()) != null){
                sb.append(line + NL);
            }
            in.close();

            String page = sb.toString();
            Log.d("Join", page);

如果你只想获取 xml 数据,你最好扩展 DefaultHandler。并使用像 SAXParser 、XMLReader 这样的解析器。

Try this one.

HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while((line = in.readLine()) != null){
                sb.append(line + NL);
            }
            in.close();

            String page = sb.toString();
            Log.d("Join", page);

And if you want to get just xml data, you'd better extend DefaultHandler. And use a parser like SAXParser ,XMLReader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文