Frage

I'm trying to read data from a server by using a GET URLRequest with a URLLoader, and it does work. The problem is, it only works once.

public class Settings
{
    private static var request:URLRequest;
    private static var loader:URLLoader;

    public static function getRequest(url:String):void
    {
        request = new URLRequest(url);
        request.method = URLRequestMethod.GET;

        loader = new URLLoader();
        loader.addEventListener(flash.events.Event.COMPLETE, responseHolder);
        loader.load(request);
    }
}

Whenever I open the Flash application I put this in and call getRequest, I get the correct data from the URLLoader. Whenever I call it afterwards, I still get a response and an Event.COMPLETE is still dispatched, but the data received is always the data obtained from the first GET. Only if I close and re-open the application does the GET actually obtain the most recent data.

I also ran a remote debug session to test this, and it turns out that only the first call of this function even communicates with the server it's trying to read from.

The only explanation for this that I can think of is that the URLRequest and URLLoader aren't being cleared after reading the data, so when another GET is called, they simply reuse the last acquired data without trying to contact the server. But I'm clearing them, as such:

    //in Settings class
    private static function responseHolder(e:flash.events.Event):void
    {
        request = null;
        loader.removeEventListener(flash.events.Event.COMPLETE, responseHolder);
        loader.close();
        loader = null;
    }

Is there something I have to do to re-enable GET URLRequests?

War es hilfreich?

Lösung

You should try adding a random number at the end of the URL in order to prevent browser caching :

request = new URLRequest(url + "?n=" + Math.random()*10000);

I know it's ugly but it might work.

Hope that's it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top