Android 将inputStream传递给XmlParser时出现FileNotFound异常
我渴望android开发。我将SAX解析器用于xml解析器。我找不到这个例外的原因。 我尝试了getAsset()方法。但它不起作用 xmlParser代码:::Android 将inputStream传递给XmlParser时出现FileNotFound异常,android,inputstream,saxparser,filenotfoundexception,Android,Inputstream,Saxparser,Filenotfoundexception,我渴望android开发。我将SAX解析器用于xml解析器。我找不到这个例外的原因。 我尝试了getAsset()方法。但它不起作用 xmlParser代码::: public class XMLParser { public static Country parseCountry(InputStream is) { try { Country country= new Country(null, null, null); XM
public class XMLParser {
public static Country parseCountry(InputStream is) {
try {
Country country= new Country(null, null, null);
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
XMLHandler xmlHandler = new XMLHandler();
xmlReader.setContentHandler(xmlHandler);
xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));
country = xmlHandler.getParsedCountryData();
} catch(ParserConfigurationException pce) {
Log.e("SAX XML", "sax parse error", pce);
} catch(SAXException se) {
Log.e("SAX XML", "sax error", se);
} catch(IOException ioe) {
Log.e("SAX XML", "sax parse io error", ioe);
}
return country;
}
}
为什么要使用带有URL的FileInputStream?尝试:
public class XMLParser {
public static Country parseCountry(InputStream is) {
try {
Country country= new Country(null, null, null);
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
XMLHandler xmlHandler = new XMLHandler();
xmlReader.setContentHandler(xmlHandler);
xmlReader.parse(new InputSource(new URL("http://64.85.165.53/dharatest/xmlarray.xml").openStream());
country = xmlHandler.getParsedCountryData();
} catch(ParserConfigurationException pce) {
Log.e("SAX XML", "sax parse error", pce);
} catch(SAXException se) {
Log.e("SAX XML", "sax error", se);
} catch(IOException ioe) {
Log.e("SAX XML", "sax parse io error", ioe);
}
return country;
}
}
换线
xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));
借
其中,callWebservice方法如下所示
private InputStream callWebservice(String serviceURL) {
HttpClient client=new DefaultHttpClient();
HttpGet getRequest=new HttpGet();
try {
// construct a URI object
getRequest.setURI(new URI(serviceURL));
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
// buffer reader to read the response
BufferedReader in=null;
// the service response
HttpResponse response=null;
try {
// execute the request
response = client.execute(getRequest);
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException", e.toString());
} catch (IOException e) {
Log.e("IO exception", e.toString());
}
if(response!=null)
try {
return response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
return null;
return null;
}
newfileinputstream(“http://64.85.165.53/dharatest/xmlarray.xml“”
???您需要先从该url下载.xml文件,然后才能读取/解析该xml。既然您有url(“…”),为什么要使用此新方法呢?openStream()
?@A.A在我的例子中,我使用的url与上述代码不兼容,也就是说,我使用的代码如图所示,几乎适用于每个url链接
private InputStream callWebservice(String serviceURL) {
HttpClient client=new DefaultHttpClient();
HttpGet getRequest=new HttpGet();
try {
// construct a URI object
getRequest.setURI(new URI(serviceURL));
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
// buffer reader to read the response
BufferedReader in=null;
// the service response
HttpResponse response=null;
try {
// execute the request
response = client.execute(getRequest);
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException", e.toString());
} catch (IOException e) {
Log.e("IO exception", e.toString());
}
if(response!=null)
try {
return response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
return null;
return null;
}
public class XMLParser {
public static Country parseCountry(InputStream is) {
try {
Country country= new Country(null, null, null);
XMLReader xmlReader =SAXParserFactory.newInstance().newSAXParser().getXMLReader();
XMLHandler xmlHandler = new XMLHandler();
xmlReader.setContentHandler(xmlHandler);
xmlReader.parse(new InputSource(getAssets().open("data.xml"));
country = xmlHandler.getParsedCountryData();
} catch(ParserConfigurationException pce) {
Log.e("SAX XML", "sax parse error", pce);
} catch(SAXException se) {
Log.e("SAX XML", "sax error", se);
} catch(IOException ioe) {
Log.e("SAX XML", "sax parse io error", ioe);
}
return country;
}
}