I'm testing a demo program from https://www.b4x.com/android/forum/t...-local-php-mysql-server-xampp-with-b4a.48635/ upon running the given program, getting counts of records and inserting records has no problem..but I get issues when getting the data from php+mysql.
Am I missing something in the process?
Codes to get data from table persons
and this the php script
More power to this forum
Am I missing something in the process?
B4X:
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:XamppphpMysql.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Back from Job:CountP
Response from server: 3
Back from Job:InsertNewP
Response from server: "Inserted"
Back from Job:CountP
Response from server: 4
Back from Job:GetP
Response from server: []
Error occurred on line: 112 (Main)
java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
at XamppphpMysql.example.main._jobdone(main.java:539)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:668)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:334)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at anywheresoftware.b4a.BA$3.run(BA.java:334)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **
Codes to get data from table persons
B4X:
Sub GetPersonsButton_Click
Dim GetPersons As HttpJob
GetPersons.Initialize("GetP", Me)
GetPersons.download2("http://" & ServerIP & "/persons/persons.php", Array As String ("action", "GetPersons"))
End Sub
B4X:
Sub JobDone(Job As HttpJob)
ProgressDialogHide
If Job.Success Then
Dim res As String
res = Job.GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select Job.JobName
Case "GetP"
Dim ListOfPersons As List
Dim PersonName As String
Dim PersonAge As String
ListOfPersons = parser.NextArray 'returns a list with maps
Log(ListOfPersons)
PersonsListview.Clear
If ListOfPersons.Size=0 Then
PersonsListview.AddSingleLine ("No persons found...")
Else
For i = 0 To ListOfPersons.Size - 1
Dim Person As Map
Person = ListOfPersons.Get(i)
PersonName = Person.Get("name")
PersonAge = Person.Get("age")
PersonsListview.AddSingleLine (PersonName & ", " & PersonAge)
Next
End If
Case "CountP"
PersonsListview.AddSingleLine ("Persons in table: " & parser.NextValue)
Case "InsertNewP"
'Do nothing
End Select
Else
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
B4X:
<?php
$host = "127.0.0.1";
$user = "root";
$pw = "mypass";
$db = "persons";
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$action = $_GET["action"];
switch ($action)
{
case "CountPersons":
$q = mysql_query("SELECT * FROM persons");
$count = mysql_num_rows($q);
print json_encode($count);
break;
Case "GetPersons":
$q = mysql_query("SELECT name, age FROM persons");
$rows = array();
while($r = mysql_fetch_assoc($q))
{
$user = array();
foreach($r AS $name => $value){
$user[$name] = utf8_encode($value);
}
$row[] = $user;
}
print json_encode($rows);
break;
case "InsertNewPerson":
$name = $_GET["name"];
$age = $_GET["age"];
$q = mysql_query("INSERT INTO persons (name, age) VALUES ('$name', $age)");
print json_encode("Inserted");
break;
}
?>
Last edited: