B4J Question json to server and json from server

vmag

Active Member
Hello. Is it possible to send a json request to a tcp server and get a json response? For example: localhost server, port 5060, sent by json (possible in the bit string) "00 00 00 1b 7b 22 63 6f 6d 6d 61 6e 64 22 3a 22 47 65 74 44 72 69 76 65 72 49 6e 66 6f 22 7d", then the answer will be in the form of a string of bits ?
 

vmag

Active Member
Thanks! More than enough information! Excellent project B 4X, there is a solution to any question...
 
Upvote 0

vmag

Active Member
Hello.
I have studied the lesson very carefully from the link above.
Such a simple and original solution I have never met anywhere, great!
But I have a small problem with generating a data package to send to one of the servers,
I need to somehow change the way the buffer is filled in the statement
B4X:
astream.Write (Buffer As Byte) As Boolean
I'll try to explain with an example:
- You need to send a 27-character string to the server:
{"command":"GetDriverInfo"}
The buffer filling algorithm is as follows:
1. Convert the string length = 27 characters to Buffer (HEX) = 1b, add zeros up to 4 bytes and get
00 00 00 1b
2. Converting the source string to Buffer (HEX) format:
7b 22 63 6f 6d 6d 61 6e 64 22 3a 22 47 65 74 44 72 69 76 65 72 49 6e 66 6f 22 7d
3. Write to the buffer (Buffer (HEX)) final result to send to the server:
00 00 00 1b 7b 22 63 6f 6d 6d 61 6e 64 22 3a 22 47 65 74 44 72 69 76 65 72 49 6e 66 6f 22 7d
in which:
- always strictly the first 4 bytes is the length of the source string (in this case 00 00 00 1b)
- then the text of the string in Buffer (HEX) format)

Judging by the video lesson, my problem is solved by two or three operators, which I regret
I don't know yet, but I hope they will help me here as always...
 
Last edited:
Upvote 0

vmag

Active Member
Yes, it may help, in this code snippet I get a normal response from the server that it doesn't understand the format of the message I'm sending it:
B4X:
Private Sub Astream_NewData(Buffer() As Byte)
    txtAnsver.Text = BytesToString(Buffer, 0 ,Buffer.Length, "utf8")
End Sub
 
Upvote 0

vmag

Active Member
Hooray! Point 1 made:
B4X:
Sub btnnext_Click
Ints = Array As Int(27)
Conv. Little Endian = False
Bytes = Conv.IntsToBytes(Ints)
txt Int.Text=Iconv.HexFromBytes(Bytes)
End Sub
It turned out:
0000001B
 
Last edited:
Upvote 0

vmag

Active Member
Point 2 looks like you need to do one character at a time...
So from the first character { we get a 7B
B4X:
Sub btnInt3_Click
    Ints = Array As Int(Asc("{"))
    Conv.LittleEndian = False
    Bytes = Conv.IntsToBytes(Ints)
    txtInt2.Text=Conv.HexFromBytes(Bytes)
End Sub
Result
0000007B
Now it is somehow necessary to insert it into a single thread...
 
Upvote 0

vmag

Active Member
You can use B4XBytesBuilder (B4XCollection) to create an array of bytes with all the data you need and then convert to hex string it with bc.HexFromBytes.
I can now assemble the string itself without any problems, the length goes as it is, and throw out zeros from the converted characters and collect all this in a heap, but how to send this string in this form and without changes to astream.Write I don't know
 
Upvote 0

vmag

Active Member
I apologize to everyone for the time taken, the example at the top link is perfect, even my version works for sending a package in just one line.
B4X:
astream.Write(txtJson.Text.GetBytes("UTF8"))
The problem was in the last place you could think of-quotation marks ( " ) in the text of the wrong format... I started to deal with each character and it turned out the code at ( " ) is not 22. I'm sorry again, everyone...
 
Upvote 0
Top