Android Question Sending JSON data to B4J server.

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
After researching on forum I made a code work.

However, I would like something better. More "professional". More safe.

Do you have an example that sends JSON code and gets in BJ4?

Thank you.
 

OliverA

Expert
Licensed User
Longtime User
After researching on forum I made a code work.
What code and what did you get to work? Are you already sending/receiving JSON or would you like to send/receive JSON? If you're staying in the B4x environment, then why not serialize? Not everything has to be JSON.
 
Upvote 0

johndb

Active Member
Licensed User
Longtime User
After researching on forum I made a code work.

However, I would like something better. More "professional". More safe.

Do you have an example that sends JSON code and gets in BJ4?

Thank you.
If you are using a closed environment i.e Your own B4J server then I agree with @OliverA. In my B4A Client - B4J Server environment I use encrypted serialization of data (maps, lists, ...) using the RandomAccessFile library.
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
What code and what did you get to work? Are you already sending/receiving JSON or would you like to send/receive JSON? If you're staying in the B4x environment, then why not serialize? Not everything has to be JSON.

I can't put an image in serialized object. Right?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

KMatle

Expert
Licensed User
Longtime User
JSON is just text; you should convert the image to a string (base64)

Yep. That's the way I do it. I use a list with maps (can contain f.e. rows you want to insert). Convert that to a string. With that you can additionally use encryption if you need it. If you need to add binary data (like an imag) convert it to Base64 as Lucas posted.

B4X:
JsonList.Initialize
    JsonZeileMap.Initialize
    JsonZeileMap.put("Action", "Insert")
    JsonZeileMap.put("uname", "xxxx")
    JsonZeileMap.put("upw", SHA256Hash("pw"))
    JsonZeileMap.put("umail", "kk@kk.de")
    JsonZeileMap.put("myimage", SomeBase64String)
     
    JsonList.add(JsonZeileMap)
 
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize2(JsonList)
 
    Dim JSONstring As String
    JSONstring = JSONGenerator.ToString
    Log(JSONstring)

    Dim LoginJob As HttpJob
    LoginJob.Initialize("InsertNew", Me)
    LoginJob.PostString(Servername & "/login/loginpre.php", JSONstring)

Benefits:

This solution (JSON, Base64, lists, maps) is compatible with B4x AND all other platforms (.net, PHP, etc.) as it is just a string and a list with maps is just an array containing arrays.

PHP:

B4X:
$json = file_get_contents("php://input");

    $jsall = array();
    $jsone = array();

    $jsall=json_decode($json, true); //get the LIST

    $jsone=$jsall[0]; //first MAP of the LIST

    //get contents of the map (looks like map.get("umail")
    $umail=mysqli_real_escape_string($con,$jsone["umail"]);
    $upw=mysqli_real_escape_string($con,$jsone["upw"]);
    $uname=mysqli_real_escape_string($con,$jsone["uname"]);


This is a simple example. In my apps I use encryption (AES to be compatible with other platforms). As "JSONSTRING" is a simple string, you can encrypt it very easy.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…