URLConnection on Android App

I am using a Macbook pro M1 and Xojo 2023r4
Just for the pourpose of this post in order to simplify the explanation of my problem, I had created a middleware in order to work as a web service communication software in a Server to serve an Android App.
To debug the Android App I run the middleware in my Mac as Localhost in the port 8080
I also wrote a Desktop App as a testing App of the middleware.
And I simplified the code to minimun for this communication testing and example.

The code of the middleware for the Web Service includes:
The handleURL event:

A web service function:

On the testing Desktop App:
the method calling the web service is:

and the ContentReceived event of the URLConnection:

As is expected, when the Desktop App stops at the break, the JSON given by the Web Service as response was received fine:

I also test the middleware with a browser:


and as expected the JSON was displayed fine as above.

But from the Android App, using identical Calls as you could see bellow:

I receive the following error in the URLConnection Error Event

Quite wired is that the Call that works fine in a desktop App and the URL called in the browser works and the one in the Android App not.

Some one could tell me what I am missing ?
Thanks

127.0.0.1 is the local machine, the Android emulator is not running the web service so that won’t work. You’ll need to use the ip address of the host.

2 Likes

Thanks Wayne…
To be clear: the web service IS running on my machine, the same one that is running the android App and the emulator…
That is why I am calling the localhost = 127.0.0.1
And as you see in my post the desktop app is working fine in the same machine against the same web server app.
May be you are saying me that emulator do not work with a localhost web service ??? Is that what you suggest ???

I concur with Wayne.

The emulator creates its own entire environment. 127.0.0.1 inside the emulator is localhost on the device inside the emulator. It is not your development machine.

You can do more reading on this matter by using your favorite search engine. Unfortunately I don’t have any reference material for you, I simply recall this having come up before (and doing my own searching then).

If I’m remembering correctly, there’s a setting on the emulator for what the local address of the host is.

1 Like

You have an ‘l’ between // and 127. But I don’t think that is the whole problem.

2 Likes

@AlbertoD … yes you are right, but as you said I write it well and still the same

@Tim_Parnell & @Wayne_Golding … I catch now what you both said about the way emulator works… I will try to find a solution for this if that solution exists…

You need to use the true IP address of the host computer.

1 Like

Found and solved… for any that wants to try something similar that what I am doing, emulator uses IP http://10.0.2.2:8080 instead of localhost or 127.0.0.1