URLConnection used for REST-API

Hello,

I tried to use a REST-API to add forex chart data in a sqlite database. The API-Call is in a method of a Window, the URLConnection (Socket) is a control of the same window.

The REST-API-Call works fine, but I would like to use many calls in series, like this:

RESTdataRequest(“EUR”,“USD”,“15min”)
RESTdataRequest(“AUD”,“USD”,“15min”)
RESTdataRequest(“USD”,“CHF”,“15min”)

When firing an API-Call it takes time until content is received (contentReceived method of the URLConnection), but (I think) XOJO still is (may be) firing the next RESTdataRequest. Nevertheless an error occurs:

“An exeption of class UnsupportedOperationExeption was not handled. The application must shut down. Exception Message: A request is already in progress.”

What is the best way to avoid this error message?
What mechanism should I use?
Do you have code examples?

Kind regards from Germany,
Clemens

I assume that you are calling the next request method from within the datareceived event handler.

You’ll need to decouple that call by using for example Timer.CallLater(0, AddressOf RESTdataRequest, Argument). Argument needs to be a single variant, so you’ll need to either create a class to hold your 3 arguments or use a dictionary and cast it as a variant.

Another option would be to add a method that calls the request and a property that records the index of the request queue then you could use Timer.CallLater(0, AddressOf Method) which would invoke the next request.

HTH
Wayne

You should use one instance of URLConnection per request. In the past re-using a URLConnection caused unexpected behaviors. Subclassing URLConnection will help you significantly.

1 Like

Thanks Tim, do you can provide a snippet? How to create subclassing an use it?

Building REST API communications is a part of my business, so I don’t offer my libraries and tools for free on the forum. I am happy to offer design advice, but actual implementations are kind of what I do to pay for insulin.

Subclassing can help in many ways. If the remote API requires authentication signatures, you can build that into your class. A subclass can also help centralize error handling. The documentation will tell you about subclassing here

As individual answers to your initial questions:

Use one URLConnection per request.

An array to hold requests in progress. Best practice would be to clean up the completed requests when they finish.


If you get stuck again specific questions like the ones you have asked are great! If you have an API you’d like to interact with and just want it built and done, I do have availability for new consulting projects.

1 Like

Thanks, you tip was great, now it works fine (without an array).