Android Tutorial httputils2: Send a large array as a JSON string via Job.Download2 to a php script

Discussion in 'Tutorials & Examples' started by KMatle, Feb 19, 2015.

  1. KMatle

    KMatle Expert Licensed User

    EDIT: Use the PostString method as it can carry more data (5 MB is ok - it's a server parameter). Download2 uses the "get-method" which is limited. To retrieve the data in php use:

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

    $json = $_GET["MyJSON"];

    Job.Download2 is used to call a php script to send data like this:

    Dim Job1 As HttpJob
    "MyJob", Me)
    "",  Array As String(P1, P2, P3, P4))
    Imagine you have a List with colums and rows (=array) and want to send the content to a php script to insert the values in a MySql database.

    You could send "one row" with one Job which is not very nice. One job of x could crash on it's way and it is complicated to handle.

    So why not pack all the rows (array) in a JSON string and encode it later with php with ONE job?

    In B4A:

    Dim a List we add maps to (a map is one row)

    Dim JSONList As List
    In a loop get all the values you need and put them into a new map (=row). Add that map to the list.

    Dim ZeileMap As Map
    "Tisch", tisch.SelectedItem)
    "AID", ArtikelID)
    "PosNr", P)
    "Zeit", zeit)
    "Posten", postKlar)
    "Preis", Preis)
    "Mwst", mwst)
    Dim a JSONGenerator and Initialise it with the list above.

    Dim JSONGenerator As JSONGenerator
    Call the JsonGenerator to create a JSON string which contains our data

    Dim JSONstring As String
       JSONstring = 
    The result looks like:

    [{"Kommentar":"","Mwst":"19.00","Zeit":"20150219221021","AID":"7","PosNr":1,"Besteller":"Gabi","Posten":"Bier","Preis":3,"Tisch":"1"},{"Kommentar":"","Mwst":"19.00","Zeit":"20150219221021","AID":"1","PosNr":2,"Besteller":"Gabi","Posten":"Cola (normal)","Preis":3,"Tisch":"1"},{"Kommentar":"","Mwst":"19.00","Zeit":"20150219221021","AID":"2","PosNr":3,"Besteller":"Gabi","Posten":"Kaffee","Preis":5,"Tisch":"1"},{"Kommentar":"","Mwst":"19.00","Zeit":"20150219221021","AID":"6","PosNr":4,"Besteller":"Gabi","Posten":"Wasser (ohne)","Preis":3,"Tisch":"1"}]
    Dim J1 As HttpJob
    "J1", Me)
    "", _
    Array As String("MyJSON", JSONstring  ))
    In the php script:

    1. The JSONString is encoded into $jsall. It's an big array (=list) which contains arrays (maps) (like our list containing maps)
    2. In a loop we get each "map" from $jsall and put it to another array (single map) called $jsone
    3. From here we have access to the single values by the key (f.e. "AID")

    $json = $_GET["MyJSON"];
            $jsall = 
            $jsone = 
            $x = 
    while($x < count($jsall)) {
                $tisch = $jsone[

                *handle the data here*       

    Dependencies: HTTPUTILS, JSON
    Tags: Json, MySql, php, httputils, Job.Download2
    Last edited: Dec 1, 2016
    Segga, Dadaista, basicuser27 and 11 others like this.
  2. Ed Brown

    Ed Brown Active Member Licensed User

    This is exactly what I've been looking for!
    Thanks @KMatle for a great job in explaining how to create a JSON string.
    KMatle likes this.
  3. aeric

    aeric Active Member Licensed User

    I am not sure I should bump this old thread. I am running into an error "Request-URI Too Long" when sending a long json data to the web service.

    From the link below, it seems I need to modify the LimitRequestLine in Apache conf:

    Here is my code
    ' Used to Add New / Update Existing Notes to Cloud
    Sub UploadNotes
    Dim cur As Cursor
    Dim qry As String
    Dim jsn As String
    Dim gen As JSONGenerator   
    Dim L1 As List
    Dim M1 As Map
        qry = 
    "SELECT * FROM MyNotes ORDER BY id"
        cur = DB.ExecQuery(qry)
    For i = 0 To cur.RowCount - 1
            cur.Position = i
    "id", cur.GetInt("id"))
    "aa", cur.GetString("aa"))
    "bb", cur.GetString("bb"))
    "cc", cur.GetString("dd"))
    "ee", cur.GetInt("ee"))
        jsn = gen.ToString
    Dim HJ As HttpJob
    Dim strURL As String   
    "UploadNotes", Me)
        strURL = ServerURL & 
    Array As String("ui", gUser, "pw", gPassword, "json", jsn))
    Log("Error: " & LastException.Message)
    ToastMessageShow("Error: UploadNotes"False)
    End Try
    End Sub
    Please advice.
  4. Erel

    Erel Administrator Staff Member Licensed User

    You shouldn't. Please start a new thread in the questions forum.
  5. aeric

    aeric Active Member Licensed User

    Thanks Erel.
    I created a new thread here
  6. Neojoy

    Neojoy Member Licensed User

    Is there some limit to Map list or JSONList?
  7. KMatle

    KMatle Expert Licensed User

    Just the memory on the app side. On the server side there's a parameter (mine has 5 MB). You can compress it, too.

    I use it a lot to upload f.e. pictures (300 KB) and corresponding data with encryption. If you need huge data streams better use FTP.
  8. Neojoy

    Neojoy Member Licensed User

    Thanks for your explanation, it was that I needed to know :)
  9. kostefar

    kostefar Active Member Licensed User

    I´m struggling a bit with this. I´ve followed the instructions given, I believe, but end up with a weird result. This is where it gets interesting:

    Dim dastring As String
    Dim postmessagejob As HttpJob
    dastring = value
    "postmessagejob", Me)
    Log (dastring)
    Log ("jsonarray",dastring)
        postmessagejob.download2(MainURL &  
    "post_message.php?",Array As String("jsonarray",dastring))
    The log says:

    The reason why I added the logging lines was because nothing happened on the server side, so I decided to see what is being sent. As far as I can see, the string to be sent, which has been json encoded as instructed, looks formatted correctly, but Log ("jsonarray",streng) shows nothing like it.
    On the server side, indeed I have
    $jsonarray = $_GET["jsonarray"];
    followed by matches for my custom parameters.

    Any clues?

    Thanks in advance!
    Last edited: Jan 5, 2017
  10. KMatle

    KMatle Expert Licensed User


    $json = file_get_contents("php://input");
    If "nothing happens" just use a print command in the php to see what the variables contain. In JobDone see what it responds.
    kostefar likes this.
  11. kostefar

    kostefar Active Member Licensed User

    Thanks, but I decided to just post it like (parametervalues,"") so that it´s all one big URL. That works fine, and now I´m just wondering if there are any issues with using this method, such as a limitation to the length of the URL?
  12. KMatle

    KMatle Expert Licensed User

    It's a server parameter. Mine has 5 MB which should be more than enough for just JSON text data. Usually you will not upload that many of data from a "small" device. If you need to upload big files switch to multipart upload (browse the forum for that).
  13. kostefar

    kostefar Active Member Licensed User

    Thanks KMatle, I´ll check with my host what their limit is.
    So is 5 mb limit regardless of whether if you use the method I´m now using, or is it more if you use
    Array As String("MyJSON", JSONstring  ))
  14. KMatle

    KMatle Expert Licensed User

    kostefar likes this.
  15. kostefar

    kostefar Active Member Licensed User

  16. kostefar

    kostefar Active Member Licensed User

    So I just tried sending a json array with a base64 encoded image of 103 kb in it. Looks good in b4a, but from the server I get "
    Request-URI Too Large
    The requested URL's length exceeds the capacity limit for this server."

    The php limit is set to 20M by default, so it seems to be my provider, ipage, that´s got a limit in their server nginx.conf. I´ve already been in touch with them, and it took me 45 mins to make the guy understand that it´s not an email I´m trying to send, and that he´s wrong when he calls the base64 encoded stuff "junk characters" - it´s exactly the opposite. Anyway, he´d contact some tech specialists from which I haven´t heard yet, but in the meantime I´d be curious to know if anybody else here had such a problem, and if they found any workarounds for it. I tested to see when the error occurs, and it´s when bytes being sent is above 8200 - strange number indeed.
  17. DonManfred

    DonManfred Expert Licensed User

    dont send the jsonstring as get parameter.... Send it as poststring (if the server accept it) or using post-variables (multipartpost without sending files)

    Something like:
    Dim m As Map
        Job.PostMultipart(MainURL &  
    in post_message.php you get the value with
    $json $_POST["jsonarray"];
    I suggest to create a new thread for this issue
    Last edited: Jan 16, 2017
    KMatle, Erel and kostefar like this.
  18. kostefar

    kostefar Active Member Licensed User

    Thanks Master Manfred, I´ll try with $_POST in php instead and otherwise try with multipartpost. If this does not work, indeed I will create a new thread.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice