在我的 android 项目中,我正在读取一个 xml 文件,该文件由一个 python 脚本不断更新。对于更新数据,我不断读取 xml 文件,每当通过 python 脚本在 xml 文件中更新数据时,我都会在我的应用程序中使用它。
但现在的问题是,由于 python 脚本和我的 android 项目连续访问一个 xml 文件(一个资源所以我认为并发问题),从 xml 文件获取数据并通过 python 将数据写入 xml 文件需要花费很多时间脚本。
那么,有没有什么简单的方法可以在 python 脚本和 android 应用程序之间进行通信,这样我就可以避免使用 xml 文件并直接使用 python 脚本发送给我的数据? 而且它让我的执行速度更快。
我试过了,(这是我用于访问 xml 文件的 android 代码)
public void getData() throws Exception {
try
{
while(!isStop)
{
isStop=parseXmlData();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
private boolean parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
boolean flag=false;
try {
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
File file=new File("/mnt/sdcard/xmldata.xml");
FileInputStream fin=null;
if(file.isFile())
{
try
{
fin=new FileInputStream(file);
}catch(Exception e)
{
}
}
dom = db.parse(fin);
flag = parseDocument();
// from this I am getting last xml value "stop" then it returns
true and from while loop I am exited..
}catch(ParserConfigurationException pce) {
//pce.printStackTrace();
}catch(SAXException se) {
//se.printStackTrace();
}catch(IOException ioe) {
//ioe.printStackTrace();
}
return flag;
}
编辑:在我看来,我有三个选择,
- 使用管道
- 套接字通信
- AIDL
那么哪个更好,为什么?
如果我出错了,请指导我。谢谢
请您参考如下方法:
