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:
instead of
Job.Download2 is used to call a php script to send data like this:
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)
In a loop get all the values you need and put them into a new map (=row). Add that map to the list.
Dim a JSONGenerator and Initialise it with the list above.
Call the JsonGenerator to create a JSON string which contains our data
The result looks like:
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")
Dependencies: HTTPUTILS, JSON
Tags: Json, MySql, php, httputils, Job.Download2
B4X:
$json = file_get_contents("php://input");
instead of
B4X:
$json = $_GET["MyJSON"];
Job.Download2 is used to call a php script to send data like this:
B4X:
Dim Job1 As HttpJob
Job1.Initialize("MyJob", Me)
Job1.Download2("http://MyDomain.com/MyFolder/MyPhpScript.php", 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)
B4X:
Dim JSONList As List
JSONList.Initialize
In a loop get all the values you need and put them into a new map (=row). Add that map to the list.
B4X:
Dim ZeileMap As Map
ZeileMap.Initialize
ZeileMap.put("Tisch", tisch.SelectedItem)
ZeileMap.put("AID", ArtikelID)
ZeileMap.put("Besteller",LoggedInUser)
ZeileMap.put("PosNr", P)
ZeileMap.put("Zeit", zeit)
ZeileMap.put("Posten", postKlar)
ZeileMap.put("Preis", Preis)
ZeileMap.put("Mwst", mwst)
ZeileMap.put("Kommentar",BArtikelKommentar(i))
JSONList.add(ZeileMap)
Dim a JSONGenerator and Initialise it with the list above.
B4X:
Dim JSONGenerator As JSONGenerator
JSONGenerator.Initialize2(JSONList)
Call the JsonGenerator to create a JSON string which contains our data
B4X:
Dim JSONstring As String
JSONstring = JSONGenerator.ToString
The result looks like:
B4X:
[{"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"}]
B4X:
Dim J1 As HttpJob
J1.Initialize("J1", Me)
J1.Download2("http://192.168.178.21/MyFolder/MyPhp.php", _
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")
B4X:
$json = $_GET["MyJSON"];
$jsall = array();
$jsone = array();
$jsall=json_decode($json, true);
$x = 0;
while($x < count($jsall)) {
$jsone=$jsall[$x];
$tisch = $jsone["Tisch"];
$besteller=$jsone["Besteller"];
$posnr=$jsone["PosNr"];
$posten=$jsone["Posten"];
$preis=$jsone["Preis"];
$mwst=$jsone["Mwst"];
$aid=$jsone["AID"];
$zeit=$jsone["Zeit"];
$kommentar=$jsone["Kommentar"];
*handle the data here*
$x++;
}
Dependencies: HTTPUTILS, JSON
Tags: Json, MySql, php, httputils, Job.Download2
Last edited: