Hi all,
I started on my PC an http server to serve a directory.
In Windows prompt I write this command: (for convenience I use python command line)
I just used python command line to test, but next I will use B4J with http server.
Now I want download a file from B4A with OkHttpUtils2 and HttpJob.
Because I will test on Android Emulator, initially I had troubles because I've used 'localhost' or 127.0.0.1 in my requests,
and this cannot work because it refer to the same android device and not to a localhost of host machine.
I works a lot with networks, but I learned this on this thread thanks to @aeric tip.
https://www.b4x.com/android/forum/threads/httpjob-not-connecting-to-localhost.145737/#post-989752
Now that I remember I had the same issue on VirtualBox in past.
As expected this don't worked because different loopbacks and python server on host machine always refused to connect.
Now, searching on the web, I found a tip to use 10.0.2.2 as IP loopback to connect the emulator to the host machine.
This works now, and the emulator can connect to the python server started on the host machine.
Here a code:
Now it can connect to the server and I see the log on the command prompt that show served file:
Note that I placed in the manifest this:
Please, can someone help me to know what happen here and because this error appear ?
Thanks
I started on my PC an http server to serve a directory.
In Windows prompt I write this command: (for convenience I use python command line)
and python reply this:PS C:\Users\Massimo\Downloads\three.js-master> python -m http.server
started to serving a three.js-master directory on localhost:8000 as default port.Serving HTTP on :: port 8000 (http://[::]:8000/) ...
I just used python command line to test, but next I will use B4J with http server.
Now I want download a file from B4A with OkHttpUtils2 and HttpJob.
Because I will test on Android Emulator, initially I had troubles because I've used 'localhost' or 127.0.0.1 in my requests,
and this cannot work because it refer to the same android device and not to a localhost of host machine.
I works a lot with networks, but I learned this on this thread thanks to @aeric tip.
https://www.b4x.com/android/forum/threads/httpjob-not-connecting-to-localhost.145737/#post-989752
Now that I remember I had the same issue on VirtualBox in past.
As expected this don't worked because different loopbacks and python server on host machine always refused to connect.
Now, searching on the web, I found a tip to use 10.0.2.2 as IP loopback to connect the emulator to the host machine.
This works now, and the emulator can connect to the python server started on the host machine.
Here a code:
B4X:
Private Sub Button1_Click
DownloadAndSaveFile ("http://10.0.2.2:8000/examples/models/gcode/benchy.gcode")
End Sub
Sub DownloadAndSaveFile (Link As String)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
' j.GetRequest.SetHeader("Access-Control-Allow-Origin", "true")
' j.GetRequest.SetHeader ("ContentType", "text/plain")
j.GetRequest.SetHeader ("ContentType", "application/octet-stream")
' j.GetRequest.SetHeader ("Host", "exampleproject.com")
' j.GetRequest.SetHeader ("Connection", "close")
Wait For (j) JobDone(j As HttpJob)
Dim map As Map = j.Response.GetHeaders
For i = 0 To map.Size - 1
Log("HEADER: " & map.GetKeyAt(i) & ": " & map.GetValueAt(i))
Next
Log(" ")
Log("Success: " & j.Success)
Log("StatusCode: " & j.Response.StatusCode)
Log("ErrorResponse: " & j.Response.ErrorResponse)
Log(" ")
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
For Each f As String In File.ListFiles(File.DirInternal)
Log("File: " & f)
Next
End Sub
And this is the B4A log, here the http code is 200, it even have content length:::ffff:127.0.0.1 - - [24/May/2024 20:56:28] "GET /examples/models/gcode/benchy.gcode HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [24/May/2024 21:22:17] "GET /examples/models/gcode/benchy.gcode HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [24/May/2024 21:24:16] "GET /examples/models/gcode/benchy.gcode HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [24/May/2024 21:25:22] "GET /examples/models/gcode/benchy.gcode HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [25/May/2024 09:55:52] "GET /examples/models/gcode/benchy.gcode HTTP/1.1" 200 -
The problem now is that the HttpJob fails with this error:** Activity (main) Pause, UserClosed = false **
** Activity (main) Create (first time) **
** Activity (main) Resume **
*** Receiver (httputils2service) Receive (first time) ***
HEADER: content-length: [1940664]
HEADER: content-type: [application/octet-stream]
HEADER: date: [Sat, 25 May 2024 11:14:24 GMT]
HEADER: last-modified: [Fri, 26 Apr 2024 07:52:13 GMT]
HEADER: server: [SimpleHTTP/0.6 Python/3.11.5]
Success: false
StatusCode: 200
ErrorResponse:
ERROR: java.net.ProtocolException: unexpected end of stream
File: virtual_assets
I've tried to manage headers but the result not changed.ERROR: java.net.ProtocolException: unexpected end of stream
Note that I placed in the manifest this:
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)
Please, can someone help me to know what happen here and because this error appear ?
Thanks
Last edited: