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

时间:2010-12-15 19:56:49

标签: android xml parsing cookies sax

我想使用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?我知道我可以像这样向HttpClients添加Cookies:

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的手册?

1 个答案:

答案 0 :(得分:1)

步骤1:使用HttpClient将XML作为字符串检索,使用您想要的任何cookie

步骤2:使用SAX解析由HttpClient

检索的字符串