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.
 
B4J? Shouldn´t the thread created in the B4J Forum then?

Posting the json is the same in B4A/B4J... There are a lot of examples in the forum. Just search for them.
What you need is to know how to get it in B4J.
 
Upvote 0
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
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
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
Upvote 0
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.

Code:
JsonList.Initialize
    JsonZeileMap.Initialize
    JsonZeileMap.put("Action", "Insert")
    JsonZeileMap.put("uname", "xxxx")
    JsonZeileMap.put("upw", SHA256Hash("pw"))
    JsonZeileMap.put("umail", "[email protected]")
    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:

Code:
$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
Back
Top