In my android project I am reading a xml file which are continuously updated by one python script. and for update data I am continuosly reading the xml file and whenever the data is updated in xml file by python script I used that in my app.
But now problem is, because of python script and my android project continuosly accessing one xml file (One resource so I think concurrency problem), it takes so much time to get data from xml file and write data to xml file through python script.
So, Is there any simply way to communicate between python script and android app so I will avoid the use of xml file and directly used the data send to me by python script? And also it gives me faster execution.
I tried this, (This is my android code for accessing xml file)
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;
}
EDIT: In my mind I have three options,
- Using Pipe
- Socket Communication
- AIDL
So which one is better and why?
If Am going wrong then please guide me. Thanks
You could try using Sockets to communicate. http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html
精彩评论