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

max123

Well-Known Member
Licensed User
Longtime User
I know this thead is an old thread, but solved in part my problem I had for 3 days.
So may this can be useful for other users have the same problem.

Many thanks @aeric for the tips.

I have launched an http python server to serve files in local, just on a served folder of my pc I launched python -m http-server
From my tests on android emulator using localhost:8000 httpjob always refused a connection, after this I tried 127.0.0.1:8000 and same, didn't worked.

Now thanks to you I realized that localhost of emulator is not the same localhost of my pc, but just point to android device itself, in this case the emulator.

Searching on the web now I found a tip to use 10.0.2.2 to point pc localhost, and that works.
Now I can connect from emulator to my pc localhost but for some strange reasons I cannot download the served python http-server files.

Here my code that now connect after changing localhost to 10.0.2.2
B4X:
DownloadAndSaveFile ("http://10.0.2.2:8000/examples/models/gcode/benchy.gcode")

Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
'    j.GetRequest.SetHeader("Access-Control-Allow-Origin", "*")
 
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "benchy.gcode", False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
        Log("DONE")
    Else
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release
End Sub
And this is the httpjob error:
ERROR: java.net.ProtocolException: unexpected end of stream
on server side, on the command prompt I see connection, but python says file not found, but pointing with a browser it ask to download and I can download it.
::1 - - [24/May/2024 18:55:44] code 404, message File not found
::1 - - [24/May/2024 18:55:44] "GET /examples/models/gcode/benchy.gcode%22 HTTP/1.1" 404 -
Note that I have this on the manifest:
B4X:
' 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)
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I know this thead is an old thread, but solved in part my problem I had for 3 days.
So may this can be useful for other users have the same problem.

Many thanks @aeric for the tips.

I have launched an http python server to serve files in local, just on a served folder of my pc I launched python -m http-server
From my tests on android emulator using localhost:8000 httpjob always refused a connection, after this I tried 127.0.0.1:8000 and same, didn't worked.

Now thanks to you I realized that localhost of emulator is not the same localhost of my pc, but just point to android device itself, in this case the emulator.

Searching on the web now I found a tip to use 10.0.2.2 to point pc localhost, and that works.
Now I can connect from emulator to my pc localhost but for some strange reasons I cannot download the served python http-server files.

Here my code that now connect after changing localhost to 10.0.2.2
B4X:
DownloadAndSaveFile ("http://10.0.2.2:8000/examples/models/gcode/benchy.gcode")

Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
'    j.GetRequest.SetHeader("Access-Control-Allow-Origin", "*")
 
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "benchy.gcode", False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
        Log("DONE")
    Else
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release
End Sub
And this is the httpjob error:

on server side, on the command prompt I see connection, but python says file not found, but pointing with a browser it ask to download and I can download it.

Note that I have this on the manifest:
B4X:
' 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)
The right way is to create a new thread. If a thread is solved then it can mark as solved.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I wrote here because searching on the forum my issue, the forum get me this page, and because I've not read solved in the title.
Anyway thanks.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I wrote here because searching on the forum my issue, the forum get me this page, and because I've not read solved in the title.
Anyway thanks.
I mean this is a thread created by another member. You can't mark the answer.

You are having a different issue which is not related to this thread.

It is not right to post in old thread. It makes the thread becomes long.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Upvote 0
Top