Android Question How to send and receive from http Server

arna1385

Member
Hi every one
I have a VB.Net program but i can't translate this part of program to B4A
In this program connect to the server with NtripCaster and NtripPort
and then send username and password and then request for answer from Server (Server response)
How can I send and receive this information with B4A?

Dim sckt As Net.Sockets.Socket

'Connect to server
sckt = New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
Try
'sckt.Connect(New Net.IPEndPoint(NTRIPCaster, NTRIPPort))
sckt.Connect(NTRIPCaster, NTRIPPort)
End Try


'Build request message
Dim msg As String = "GET /" & NTRIPMountPoint & " HTTP/1.0" & vbCr & vbLf
msg += "User-Agent: "MY Messages" & vbCr & vbLf
msg += "Accept: */*" & vbCr & vbLf & "Connection: close" & vbCr & vbLf
If NTRIPUsername.Length > 0 Then
Dim auth As String = ToBase64(NTRIPUsername & ":" & NTRIPPassword)
msg += "Authorization: Basic " & auth & vbCr & vbLf
End If
msg += vbCr & vbLf

'Send request
Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(msg)
sckt.Send(data)
Threading.Thread.Sleep(100)

'Wait for response
Dim responseData As String = ""
Try
For i = 0 To 300 'Wait 30 seconds for a response
Threading.Thread.Sleep(100)
Dim DataLength As Integer = sckt.Available
If DataLength > 0 Then
Dim InBytes(DataLength - 1) As Byte
sckt.Receive(InBytes, DataLength, Net.Sockets.SocketFlags.None)
responseData = System.Text.Encoding.ASCII.GetString(InBytes, 0, InBytes.Length)
End If
If responseData.Length > 0 Then Exit For
Next
End Try
 

MicroDrie

Well-Known Member
Licensed User
Hi arna1385,

Let me try to help you find your way on this forum with some tips.
  • Put your code between the </> tags. This will format your code.
  • You can search the forum. As an example, search for “literal string” click on the magnifying glass. This give as first result [B4X] Smart String Literal - Erel . When you click on [B4X] Smart String Literal - Erel then after reading you can change the msg string to something's like this:
msg replaced by Smart String Literal:
'Build request message
      Dim msg As String = $"
GET /${NTRIPMountPoint} HTTP/1.0
User-Agent: ${MY Messages}
Accept: */*
Connection: close"$
  • You can also search for solutions with a term such as "websocket". In this way and by studying many examples, you can come to a solution yourself. If it doesn't work, it's best to make a small example project. You can export this project via the menu file export as a zip file and add it to your question.
Good luck with your journey of discovery in the B4X development world
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
in theory, if all you want to do is make a GET request
to an HTTP(S) server and wait for some reply, you can
do that easily with the okhttputils2 service and the okhttp
library. but under normal circumstances, i don't see the
point of such a standalone operation. you get your answer,
but then what?

android supports gnss already. in fact, it's mandatory with
recent devices and versions of the os. i'm wondering if
you're writing something which is already built into your
device. also, i believe member agraham has an unpdated
gps library taking this into account.

the okhttputils2 approach is about, like, 4 lines of code with
pretty much everything you need. there is also async sockets,
but not applicable just to receive a simple answer to a single
GET. i can't help but think you've got something else going
on, eg some kind of real time communication between your
app and a gnss service. if that's the case, i believe this is
already doable with android and without any connection to
a dedicated server. you would use agraham's gps library
(it's called gps, but android uses gnss)
 
Upvote 0

arna1385

Member
Thanks for the all replies
My problem is not in creating the message
The biggest problem is not receiving the message sent from the server In the following program, I was able to create a sent message, but I could not get a response by sending a message I do not know if the message was not sent or I will not receive a reply.
 

Attachments

  • ClientSide.zip
    37 KB · Views: 131
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
the message was not sent.
 
Upvote 0

arna1385

Member
Thanks to the people who helped
The main problem in sending message is related to the programmer structure in VB.net, the phrase is related to in B4A, this phrase is related It was corrected by adding chr(13) the sent message and the server responded correctly.
in VB.net VbCrLf = chr(13) & chr(10)
in B4A CRLF = chr(10)
The problem was solved by adding chr(13) before chr(10) in the message.
This discrepancy must be rectified by the authors of the B4A.
 
Upvote 0
Top