Android、SAX 和 Cookie:在尝试从 URL 解析 XML 时如何添加 cookie
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
步骤 #1:使用
HttpClient
以字符串形式检索 XML,使用任何您想要的 cookie步骤 #2:使用 SAX 解析
HttpClient
检索到的字符串Step #1: Use
HttpClient
to retrieve the XML as a string, using whatever cookies you wantStep #2: Use SAX to parse the string retrieved by
HttpClient