Android Tutorial [B4x]Data exchange (B4x, php, servers, .net and others)

After some years I have developed several apps to communicate with other apps, servers and platforms. My goal was to have ONE standard solution without additional propriatary solutions which work only on platform a but not on b. So I've ended up with lists and maps converted from/to JSON strings.

Why to a string? Because it's simple to handle. Strings can be converted very easy to bytes if you need it encrypted. The result (because encryption workds with bytes) can be converted to a string again (Base64) and you're good.

Why lists and maps? Because you can have complex and flexible structures which then can be handled as a string which can easily exchanged between platforms. Lists and maps are just arrays which can be "understood" by all programs. Same for Base64 and JSON.


Dim Parms As Map
    Parms.put("uname", "Smith")
    Parms.put("upw", "12345")
    Parms.put("api-key", "9999")

Dim FileBuffer(0) As Byte 
FileBuffer=Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
Dim B64String As String = su.EncodeBase64(FileBuffer)
Parms.put("myfile", B64String)

    Dim Alist As List
    Dim JG As JSONGenerator

What do we have here? We define a list and a map. The map is filled with some data and even a file converted to a base64 string. As you see we've combined complex data (a file is complex) but as you see it is very simple.

Now we can decide what we need: Use it is, convert it to a Base64 string (to keep compatibility between platforms) or even convert it to bytes (to send it via asyncstreams or other)



you get bytes. With encryption (here I send it via asyncstreams)


Public Sub SendData (data() As Byte)
    If connected Then
    End If
End Sub

Via OkHttpUtils job (no encryption)

Dim JSONGenerator As JSONGenerator
    Dim JSONstring As String
    JSONstring = JSONGenerator.ToString

Dim MyJob As HttpJob
    MyJob.Initialize("GetItems", Me)
    MyJob.PostString(Servername & ScriptPath, JSONstring)

I you need to encrypt, convert the bytes to a Base64 string and send it.


- we have ONE string
- all platforms can handle JSON and Base64
- a list with maps is just an array with an array
- maps can be added to the list without changing anything else in the code

In php:

$json = file_get_contents("php://input"); 'get the string we've sent (poststring method)

    $thelist = array(); 'define an array = our list
    $onemap = array(); 'define an array = out map
    $thelist=json_decode($json, true); = our list assuming you've did a Base64 encoding before you send

    $themap=$thelist[0]; ' get the first map. We only send one but you can add several maps to the list (e.g. to insert 10 items in a db)

In (just an example to send a list with a map, see the .net docs how to use poststring)

OwnPubKey64StringExport = Convert.ToBase64String(OwnPubKeyBytes)
Dim serializer As New JavaScriptSerializer()
        Dim di As New Dictionary(Of String, String)
        di.Add("Action", "EncTest")
        di.Add("MyUsernummer", "777777")
        di.Add("MyUserMail", "nothing")
        di.Add("MyUserPW", "nothing")
        di.Add("PK", OwnPubKey64StringExport)
        serializedResult = "[" & serializer.Serialize(di) & "]" 'just a trick: [ = start of a list

        phpResult = myWebClient.DownloadString(Servername & Serverpath & "?Message=" & serializedResult)

Libs needed: