Android Question HttpJob not connecting to Localhost

DawningTruth

Active Member
Licensed User
I have a weird error and hope someone can point me in the right direction.

I have a backend API which I am running on localhost for Dev purposes. The API works perfectly when I test it using Postman.

It also used to also work perfectly when I was using B4A. Unfortunately, it just stopped working in the middle of a debugging session, for no apparent reason.

Any thoughts on what to do?


Here are the details:

Error Log:
FINISHED: ApiFetch -> Start Download Job
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Service (httputils2service) Start **
ResponseError. Reason: java.net.ConnectException: Failed to connect to /127.0.0.1:2501, Response:
FINISHED: ApiFetch -> End Download Job
Error: java.net.ConnectException: Failed to connect to /127.0.0.1:2501
ResponseError. Reason: java.net.ConnectException: Failed to connect to /127.0.0.1:2501, Response:
FINISHED: ApiFetch -> End Download Job

Manifest Editor - Workaround for localhost:
' 28 - Non-ssl (non-https) communication is not permitted by default.
' It can be enabled in B4A v9+ by adding this line to the manifest editor:
CreateResourceFromFile(Macro, Core.NetworkClearText)

HttpJob API Fetch Code:
    'POST JSON AND FETCH
    Dim apiReturn As String
    Dim httpJob As HttpJob
    httpJob.Initialize("", Me)
    httpJob.PostString(apiUrl, jsonPostValue)
    httpJob.GetRequest.SetContentType("application/json")
    httpJob.GetRequest.SetContentEncoding("gzip, deflate, br")
    
    Log("FINISHED: ApiFetch -> Start Download Job")
    Wait For (httpJob) JobDone(httpJob As HttpJob)
    Log("FINISHED: ApiFetch -> End Download Job")
    
    If httpJob.Success Then
        Log(httpJob.GetString)
        apiReturn = httpJob.GetString
    Else
        Log("Error: " & httpJob.ErrorMessage)
        apiReturn = "@network_error"
        httpJob.Release
        Return apiReturn
    End If
    httpJob.Release
 

DawningTruth

Active Member
Licensed User
how does your apiurl look like?
can you upload a small example that shows the error?
Hi Ilan,

Unfortunately, can't share API as it is confidential. But the problem is not the API it is B4A connecting to the API. It is giving a "Failed to Connect" error before it even sends the data to the API. The part I am struggling with is, why is it failing to connect?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thx Aeric,

That solved the problem.
I didn't explain what's happening.
Basically, 127.0.0.1 is the localhost IP or Loopback. Meaning, a device is calling itself. If you call 127.0.0.1 from Android, meaning it is calling itself as a local server, not calling to external party. B4A app being installed in real device or emulator is separated from the server hence have different IPs. Unless you are creating a http server inside B4A itself which is different case. Postman is running on the same machine, so it has no problem calling itself. Same to web browser able to open the website or web app hosted in localhost or B4J desktop client calling to the server on the same machine. So you should understand how the networking works between client and server.
 
Upvote 0

DawningTruth

Active Member
Licensed User
I didn't explain what's happening.
Basically, 127.0.0.1 is the localhost IP or Loopback. Meaning, a device is calling itself. If you call 127.0.0.1 from Android, meaning it is calling itself as a local server, not calling to external party. B4A app being installed in real device or emulator is separated from the server hence have different IPs. Unless you are creating a http server inside B4A itself which is different case. Postman is running on the same machine, so it has no problem calling itself. Same to web browser able to open the website or web app hosted in localhost or B4J desktop client calling to the server on the same machine. So you should understand how the networking works between client and server.
Thx for that brilliant explanation. Makes perfect sense:)
 
Upvote 0
Top