我正在转换到OKHttp,并且在我的项目中使用SAXParser。如何解析OKHttp对SAXParser的响应?或者如何使用库解析XML。一开始我就是这样做的:
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
SAXParserFactory factory1 = SAXParserFactory.newInstance();
SAXParser parser = factory1.newSAXParser();
FormHandler handler = new FormHandler();
parser.parse(inputStream, handler);
但是,使用OKHTTP,我如何将Response response = client.newCall(request).execute()
传递给XML解析器?
发布于 2014-06-28 03:12:50
你可以试试这个:
// 1. get a http response
Response response = client.newCall(request).execute();
// 2. construct a string from the response
String xmlstring = response.body().string();
// 3. construct an InputSource from the string
InputSource inputSource = new InputSource(new StringReader(xmlstring));
// 4. start parsing with SAXParser and handler object
// ( both must have been created before )
parser.parse(inputSource,handler);
PS :在您的问题中,您提到了XMLPullParser,在您的代码中,您实际上使用的是SAXParser。但是,如果您手上有xml字符串,那么这两种方法都应该做得很好。
https://stackoverflow.com/questions/24465427
复制