0

在我的应用程序上有一个按钮,它加载位于服务器上的 XML 文件。XML 文件包含 31 条信息,每天交付一条,基于天数。用户触摸一个按钮来重新加载信息,因此它每天都在变化。

这就是我坚持的事情。当应用程序从后台恢复并且我触摸按钮时,它不会提取新一天的信息;它继续显示昨天的信息。如果我强制应用程序退出(在手机上)然后重新启动,它将正确加载新一天的信息。

我的问题 - 我如何强制它在每次按钮触摸时重新加载?

感谢你的协助。这是我的 ActionScript 3 代码。

2012 年 10 月 12 日更新:我已经在几个 24 小时内对此进行了测试,发现它在 Flash 5.5 模拟器中每天都不能正确更新。所以问题可能不在于手机。我将对代码做更多的工作并报告回来,希望有一些改进。

// Main 'Today!' button function
var myDateDay:Date = new Date();
var dayNumber:int = myDateDay.day;  

myButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void {

// Opens previously saved text file to read XML URL
var file:File = File.applicationStorageDirectory;
file = file.resolvePath("qstURL.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var QSTURL:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
trace("QST URL:" + " " + QSTURL);
fileStream.close();

// Load, process main XML file
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest(QSTURL));
qstLoader.addEventListener(Event.COMPLETE, processQST);

function processQST(e:Event):void {
    qst = new XML(e.target.data);
    isProcessed();
}

//Wait until the XML is finished being processed
function isProcessed():void{
    trace("QST is processed and ready for use");

    //Clear welcome message before adding new content
    if(contains(textWelcomeMessage)){
        removeChild(textWelcomeMessage);
        removeChild(imageNoAudio);
        removeChild(textProductKeyMessage);
        trace("Welcome items removed");
    }
    else{
        trace("textWelcomeMessage not onStage");
    }

//Text Section
    // Text field formating 
    var myTextFormat1:TextFormat = new TextFormat();
    myTextFormat1.align = TextFormatAlign.LEFT; 
    myTextFormat1.size = 20; 
    myTextFormat1.font = "Verdana"; 
    myTextFormat1.color = "0x333333";
    myTextFormat1.bold = true;

    var myTextFormat2:TextFormat = new TextFormat();
    myTextFormat2.align = TextFormatAlign.LEFT; 
    myTextFormat2.size = 30; 
    myTextFormat2.font = "Verdana"; 
    myTextFormat2.color = "0x333333";

    // Text field to display title
    var title:XMLList = qst.title;
    var myText1:TextField = new TextField();
    myText1.text = title;
    myText1.autoSize = TextFieldAutoSize.NONE;
    myText1.border = false;
    myText1.width = 315;
    myText1.multiline = false;
    myText1.wordWrap = false;
    myText1.y = 125;
    myText1.x = 5; 
    myText1.setTextFormat(myTextFormat1); 

    // Text field to display question of the day
    var question:XMLList = qst.question.(@day == dayNumber).text;
    var myText2:TextField = new TextField();
    myText2.text = question;
    myText2.autoSize = TextFieldAutoSize.CENTER;
    myText2.border = false;
    myText2.width = stage.stageWidth - 60;
    myText2.multiline = true;
    myText2.wordWrap = true;
    myText2.y = 180;
    myText2.x = 30; 
    myText2.setTextFormat(myTextFormat2); 

    // Text field to display clearing statement
    var cs:XMLList = qst.clearingStatement;
    var myText3:TextField = new TextField();
    myText3.text = cs;
    myText3.autoSize = TextFieldAutoSize.CENTER;
    myText3.border = false;
    myText3.width = stage.stageWidth - 60;
    myText3.multiline = true;
    myText3.wordWrap = true;
    myText3.y = myText2.y + myText2.height + 10;
    myText3.x = 30; 
    myText3.setTextFormat(myTextFormat2); 

    //Remove existing text field to stop overlaying when button touched repeatdly
    while (numChildren > 3) removeChildAt(3);
    /Add text to stage
    addChild(myText1);
    addChild(myText2);
    addChild(myText3);

other code ....

    }//Close function "isProcessed" - load & process main XML file
} //Close function fl_MouseClickHandler - 'Today!' button
4

2 回答 2

0

I think your xml file is being cached by the OS.

If that is the case, try appending a random string at the end of your URL. Something like :

url + "?v=" + Math.ceil(Math.random()*10000)
于 2012-10-05T04:49:35.983 回答
0

我终于发现了这一点,发现一个保存日期的变量超出了主按钮的范围。因此,当单击按钮时,要重新加载所有内容,它不会重新加载日期并更新此变量。结果,天数没有更新,是用来检索每天信息的。

它怎么比这更好?

于 2012-10-18T02:29:54.087 回答