我有一個向用戶顯示資訊的應用程式。這些資訊在一個 xml 檔案中,直接嵌入到應用程式中。每年,我都必須更新 xml 檔案中的資訊,因此,我必須通過 Google Play Store 和 Apple Store 提交新的應用更新。
我想了一種方法,也許可以避免更新所有應用程式。
我在想應用程式在啟動時是否正在檢查 xml 檔案的檔案大小以及我在服務器上擁有的 xml 檔案的檔案大小。如果大小相同,則不會下載/替換檔案。如果大小不同,檔案將被下載并替換當前的 xml 檔案。
問題 :
- 即使我的應用程式當前正在使用并顯示 xml 資訊,它是否可以替換該檔案(也許 AIR 不允許它,因為它正在被應用程式讀取)?
我試過這樣做,但它不起作用:
function checking_Files():void
{
var size_of_horaires_local;
var size_of_horaires_online;
var url_local:URLRequest = new URLRequest("horaires3.xml");
var url_online:URLRequest = new URLRequest("http://****.vps.ovh.ca/horaires3.xml");
;
// Define the URLLoader.
var loader_local:URLLoader = new URLLoader();
var loader_online:URLLoader = new URLLoader();
loader_local.load(url_local);
loader_online.load(url_online);
// Listen for when the file has finished loadingloader_local_Complete
loader_local.addEventListener(Event.COMPLETE, loader_local_Complete);
loader_online.addEventListener(Event.COMPLETE, loader_online_Complete);
function loader_local_Complete(e:Event):void
{
// The output of the text file is available via the data property
// of URLLoader.
trace("size: " (URLLoader(e.target).bytesTotal));
size_of_horaires_local = URLLoader(e.target).bytesTotal;
}
function loader_online_Complete(e:Event):void{
// The output of the text file is available via the data property
// of URLLoader.
trace("size: " (URLLoader(e.target).bytesTotal));
size_of_horaires_online = URLLoader(e.target).bytesTotal;
check_if_size_different();
}
function check_if_size_different():void{
trace("checking if size is different");
if(size_of_horaires_local == size_of_horaires_online){
trace("do nothing");
log_txt.text="Do nothing, same size. Copy File to specific path";
dontWait = true;
//do nothing
}else{
log_txt.text="download the new xml file";
download_the_new_xml();
wait = true;
log_txt.text="Récupération de données en cours...";
}
}
function download_the_new_xml():void{
var urlString:String = "http://****.vps.ovh.ca/horaires3.xml";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void {
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeFile();
}
function writeFile():void {
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
wait = false;
dontWait = true;
introFinish(null);
//log_txt.text="The new XML file is written here : " file.url;
trace("The new XML file is written here : " file.url);
}
}
似乎新的xml檔案的“路徑”是錯誤的。我找不到撰寫和替換嵌入在應用程式中的舊 xml 的方法。
如果可行的話有什么想法嗎?
uj5u.com熱心網友回復:
我不相信您可以覆寫捆綁到您的應用程式中的檔案。
一種可能的解決方案是將 XML 寫入新的本地 XML 檔案并將其保存在檔案目錄或 appStorageDirectory 中,并包含一個版本號作為節點。
public static var _xmlFile:File = File.documentsDirectory.resolvePath("yourapp.xml");
private static const _initialXML:String = "<?xml version='1.0' encoding='UTF-8'?><contents><version>100</version></contents>";
//only write to local on first launch
if(!_xmlFile.exists){
_writeFileStream.open(_xmlFile, FileMode.WRITE);
_writeFileStream.writeUTFBytes(_initialXML);
_writeFileStream.close();
}
然后,每當應用程式啟動時,它都會加載本地 xml 以及來自服務器的新 xml,比較版本號并在必要時用服務器版本覆寫本地版本。
然后應用程式使用本地版本進行配置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418267.html
標籤:
