How to stop a URLConnection call before it completes and start a new one?

Another one of my “works on iOS but not on Android” posts

I am working on a URLConnection to OpenAI. The process can take a handful of seconds to complete in some cases. There can be a desire by the user to not want to wait for the first process to complete and then try to start a new call to the connection. So my question is how to I stop a URLConnection process so a new one can be initiated?

In iOS, I am using the .Disconnect method of the URLConnection class before starting the process. This works. Tried the exact same on Android, and it seems like nothing even happens (I don’t get anything back from the ContentReceived)

Then I thought, maybe .Disconnect is not the best option since the docs say

A disconnected socket is not set to Nil, but is no longer in a useful state. You should create a new socket rather than attempting to re-use a disconnected socket.

Tried first disconnecting then creating a new URLConnection, but this did not work

MyOpenAIURLConnection.Disconnect

dim connection as new OpenAIConnection

Also tried .ClearRequestHeaders since the docs say

Clears all of the request headers, which is useful if you are using the socket to send a different request.

But this also did not work, both alone and with creating a new URLConnection. Using .ClearRequestHeaders does retrieve the content for me, but I have to wait for the process to finish before starting a new request

My ultimate goal is to start a request to URLConnection, while it is still working and before the content is received, I want to stop it and start a new request. How is this achieved on Android?

Can you force it to go out of scope? I’m not familiar with Android specifics, but with a general purpose URLConnection you could try destroying the instance. I tend to stick all my connections in an array where they can persist and then remove themselves at the right time. Perhaps you could Send your URL request and immediately destroy the instance(?)

URLConnection doesn’t “die” when it goes out of scope if a request is currently waiting for a response.
It will stay in memory until it completes.

The only way to stop a URLConnection before it completes is to call Disconnect.
If that doesn’t work on Android then please file a bug report.

Ok good, so my use of this in my iOS app is correct. Now we just need to get it to work on android

I’ll make a sample project tomorrow and create an Issue. But before I do, I just want to make sure my use of Disconnect is correct. Basically, before I make the call to the API by way of the URLConnection, I disconnect the connection in case there is already one processing. The Disconnect in inside my Try Catch. I’d post my code, but I’m not at the computer. Should’ve done that earlier

Issue created:
https://tracker.xojo.com/xojoinc/xojo/-/issues/78009

I made a test app using the Using GET with URLConnection example project and simply added a Disconnect button (WebConnection.Disconnect). I just realized that after clicking Disconnect, the app freezes. Figuring this must be a bug and hopefully the Issue will get some attention

1 Like