Android Question [SOLVED] Consume REST API running on local machin

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hello colleagues

I have created a REST API with Visual Studio 2022, which connects to a local SQL Server database and creates a JSON response from the information, when executed locally, my browser looks like this in the following image:

1659292495126.png


I want to consume this REST API from B4A, I do it in the android emulator but even though my REST API is working perfectly it shows me an error saying that there is no local connection at the localhost address (127.0.0.1:5056 ).

View B4A log image
1659292648762.png


Here is my B4A code:
B4X:
Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("http://localhost:5056/Persona")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As List = parser.NextArray
        Dim data As List
        data.Initialize
        For Each colroot As Map In root
            Dim id As Int = colroot.Get("id")
            Dim nombre As String = colroot.Get("nombre")
            Dim telefono As Int = colroot.Get("telefono")
            data.Add(Array(id, nombre, telefono))
        Next
        DGVJson.SetData(data)
B4X:

I made a Desktop application consuming the REST API and it works great.

What then should I do to be able to consume this API from the Android Emulator?

I thank you very much if you can help me.
 

JohnC

Expert
Licensed User
Longtime User
Did you add the "Cleartext" line to the Manifest so that the app can make a non-HTTPS connection to your local server?

 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Did you add the "Cleartext" line to the Manifest so that the app can make a non-HTTPS connection to your local server?

1659301590466.png

Added in the manifest.
But still the same problem
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
As jashwant says, you cant use localhost in your android, as localhost means the same computer you’re running. You must use the ip of your server machine, the pc which is running the vb server. Something like 192.168.178.x:5056
Run ”ipconfig” in your pc to get the ip you must use in the android app.
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hello colleagues

I did the test connect on the mobile device and my PC with the REST API working both connected to the same WIFI LAN network

The result is that from B4A the REST API is perfectly consumed.

1659536791928.png
1659536867905.png

Here is my B4A code:
B4X:
Private Sub BtnConectar_Click
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("http://192.168.1.138:5056/Persona")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As List = parser.NextArray
        Dim data As List
        data.Initialize
        For Each colroot As Map In root
            Dim id As Long = colroot.Get("ID")
            Dim nombre As String = colroot.Get("Nombre")
            Dim telefono As Long = colroot.Get("Telefono")
            data.Add(Array(id, nombre, telefono))
        Next
        DGVJson.SetData(data)
    Else
        MsgboxAsync("Error al Conectar",":(")
    End If
    j.Release
End Sub

It remains for me to consult how to put the android emulator and my PC on the same LAN network.

Thank you all so much for your help
 
Upvote 0
Top