我的 Google 驱动器上托管了一个 XML 文件。它有一些我的 Android 应用程序运行所需的数据。因此,我决定测试是否可以从文件中获取数据并进行测试SAXParser
。但是,我得到一个SAXParseException
:
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at testing.TestSAX.processFeed(TestSAX.java:54)
at testing.TestSAX.main(TestSAX.java:40)
虽然同样的代码可以正常工作,比如 RSS 提要,但它会在 Google 驱动器文件中崩溃。
这是我打开流的方式:
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputStream stream = new URL(url).openStream();
Reader r = new InputStreamReader(stream,"UTF-8");
InputSource source = new InputSource(stream);
source.setEncoding("UTF-8");
parser.parse(source, this);
}catch(Exception e){
errorOccured = true;
e.printStackTrace();
}
我该如何解决这个问题?