简体   繁体   中英

Why am I suddenly getting stream error while using URLLoader?

I've been working on an android app project for an year and I implemented this feature in the very beginning. The app is a expense/savings management app, and it has a feature that converts the currencies in the background if user is currently in a different country. I am using geonames API to find out the name of the country. Here is the relevant code:

countryURLRequest = new URLRequest('http://api.geonames.org/findNearby?lat='+latitudes+'&lng='+longitudes+'&username=fatninja');
countryURLLoader = new URLLoader();

countryURLLoader.load(countryURLRequest);

Today, when I sent lat and lng values from the simulator I got the said stream error at this line:

countryURLLoader = new URLLoader();

What's weird is that this feature has been working fine since the beginning. I thought it might have been a bug in the code so I ran the project from backup (from 3 months ago), it's giving the same error. I know for a fact that it was working 3 months ago. What could be the problem?

This error is indicating that you're running into a problem while fetching the data. Nothing's wrong with your code. It might be that your network configuration has changed, and that's preventing access to the web service.

This is further supported by the fact that you're facing the same problem with your previously working code.

There are two things you should do.

  1. Add an IO error event handler and a HTTP status event handler.

    var loader:URLLoader = new URLLoader; loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler); // Note that the listener is added to the contentLoaderInfo object loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatusHandler); loader.load(new URLRequest("www.example.com"));

    function loader_ioErrorHandler(e:IOErrorEvent):void { // Handle the error trace(e.text); }

    function loader_httpStatusHandler(e:HTTPStatusEvent.HTTP_STATUS):void { trace(e.status); }

Add labels to notify the user about the error in the IO error handler, and maybe a button to retry the request.

  1. Increase the duration of the timeout by using the requestTimeout property of the HTTPService instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM