Android、SAX 和 Cookie:在尝试从 URL 解析 XML 时如何添加 cookie

发布于 2024-10-07 11:17:57 字数 867 浏览 1 评论 0原文

我想使用 Simple Ajax for XML 来解析特定 URL 的结果。这基本上是我的代码:

URL link = new URL(url); // url is just a string representing the url
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(myHandler);  // the class of myHandler extends from DefaultHandler
InputStream stream = link.openStream();
InputSource inputSource = new InputSource(stream);
inputSource.setEncoding("ISO-8859-1");
 xr.parse(inputSource);

但是我如何添加cookie?我知道我可以将 Cookie 添加到 HttpClient,如下所示:

BasicClientCookie cookie = new BasicClientCookie("access_token", accessToken);
mHttpClient.getCookieStore().addCookie(cookie);
HttpGet request = new HttpGet("www.reeple.net/xml/login/" + uid);
mHttpClient.execute(request);

但是如何将 cookie 添加到由 SAX-Api 处理的请求中?

I want to parse the result of a specific URL using Simple Ajax for XML. This is basically my Code:

URL link = new URL(url); // url is just a string representing the url
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(myHandler);  // the class of myHandler extends from DefaultHandler
InputStream stream = link.openStream();
InputSource inputSource = new InputSource(stream);
inputSource.setEncoding("ISO-8859-1");
 xr.parse(inputSource);

But how can I add a cookie? I know I can add Cookies to HttpClients like so:

BasicClientCookie cookie = new BasicClientCookie("access_token", accessToken);
mHttpClient.getCookieStore().addCookie(cookie);
HttpGet request = new HttpGet("www.reeple.net/xml/login/" + uid);
mHttpClient.execute(request);

But how can I add a cookie to a request, that is handlet by the SAX-Api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

过气美图社 2024-10-14 11:17:57

步骤 #1:使用 HttpClient 以字符串形式检索 XML,使用任何您想要的 cookie

步骤 #2:使用 SAX 解析 HttpClient 检索到的字符串

Step #1: Use HttpClient to retrieve the XML as a string, using whatever cookies you want

Step #2: Use SAX to parse the string retrieved by HttpClient

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