I am trying to write a simple API to allow my B4A app to get data from a MySQL database.
The problem I keep coming up against is that I cannot get B4A to correctly read the data that is sent across, I think this is due to a fundamental misunderstanding of what I am trying to achieve. Anyway here is my PHP Code:-
This is returning the data that I wanted to read in the B4A app, am I returning an array? I think so!
I have used Postman check and this is what it receives.
So it looks like I have an array.
If I make an API call from B4A I get the same data returned, however I keep getting errors in B4A.
Log("CalloutList: " & callout.Get("client_id"))
End If
Job.Release
End Sub[/CODE]
So it looks to me like B4A thinks this isn't an array, so instead I will try and treat it as an object
Now this gets rid of the error message, but the Log always says
Clearly I have no idea what i am doing, but can someone please point me in the right direction, I have been going in circles for ages with this.
The problem I keep coming up against is that I cannot get B4A to correctly read the data that is sent across, I think this is due to a fundamental misunderstanding of what I am trying to achieve. Anyway here is my PHP Code:-
PHP Code to return JSON data from MYSQL:
$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
//Check connection was successful
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
//Fetch rows from table
$result = $dblink->query("SELECT callout.id, callout.client_id, callout.time, callout.date, callout.description, callout.notes, callout.started, callout.completed, customer.first_name, customer.last_name, address.add_1, address.add_2, address.town, address.city, address.postcode FROM callout
LEFT JOIN customer on callout.client_id = customer.cust_id
LEFT JOIN address on customer.address_id = address.address_id");
//Initialize array variable
$dbdata = array();
//Fetch into associative array
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
//Print array in JSON format
echo json_encode(var_dump($dbdata));
This is returning the data that I wanted to read in the B4A app, am I returning an array? I think so!
I have used Postman check and this is what it receives.
Postman output:
array(2) {
[0]=>
array(15) {
["id"]=>
string(1) "1"
["client_id"]=>
string(4) "5072"
["time"]=>
string(5) "10:00"
["date"]=>
string(10) "20/10/2020"
["description"]=>
string(52) "Problems receiving emails from Gmail and Outlook.com"
["notes"]=>
string(47) "Come round the back and knock on the shed door."
["started"]=>
string(0) ""
["completed"]=>
string(0) ""
["first_name"]=>
string(16) "Andrew "
["last_name"]=>
string(20) "Test "
["add_1"]=>
string(25) "1 Any Stret "
["add_2"]=>
string(35) " "
["town"]=>
string(8) "Any Town"
["city"]=>
string(9) "Any Where"
["postcode"]=>
string(7) "AW1 1AW"
}
[1]=>
array(15) {
["id"]=>
string(1) "2"
["client_id"]=>
string(4) "5072"
["time"]=>
string(5) "11:00"
["date"]=>
string(10) "20/10/2020"
["description"]=>
string(67) "Problems with computer shutting down when customer is away from it."
["notes"]=>
string(28) "Customer is going out at 2pm"
["started"]=>
string(0) ""
["completed"]=>
string(0) ""
["first_name"]=>
string(16) "Andrew "
["last_name"]=>
string(20) "Test "
["add_1"]=>
string(25) "1 Any Stret "
["add_2"]=>
string(35) " "
["town"]=>
string(8) "Any Town"
["city"]=>
string(9) "Any Where"
["postcode"]=>
string(7) "AW1 1AW"
}
}
null
If I make an API call from B4A I get the same data returned, however I keep getting errors in B4A.
B4A code, errors at line 12:
Private Sub getJsonFromServer()
Dim Job As HttpJob
Dim calloutList As List
Dim callout As Map
Job.Initialize("api", Me) ' Init HttpJob
Job.PostString("https://nortechcomputers.co.uk/nortech/v1.0/api/read.php","") ' POST to url
Wait For (Job) JobDone(Job As HttpJob)
If Job.Success Then
Dim parser As JSONParser
parser.Initialize(Job.GetString)
[CODE lang="b4x" title="Line 12" highlight="1"]calloutList = parser.NextObject
End If
Job.Release
End Sub[/CODE]
java.lang.RuntimeException: JSON Array expected.
So it looks to me like B4A thinks this isn't an array, so instead I will try and treat it as an object
Use NextObject and Map:
If Job.Success Then
Dim parser As JSONParser
parser.Initialize(Job.GetString)
callout = parser.NextObject
Log("CalloutList: " & callout.Get("client_id"))
CalloutList: null
Clearly I have no idea what i am doing, but can someone please point me in the right direction, I have been going in circles for ages with this.