Android Tutorial Complete tutorial: B4A & Your own (local) php & MySql Server (Xampp) with b4a

Rafal_sdc

New Member
Licensed User
Working

$result = $polaczenie->query("SELECT name, age FROM persons");
$rows = array();
while($r = $result->fetch_assoc())
{
$rows[] = $r;
}
print json_encode($rows);
 

rjanelli

Member
Licensed User
Longtime User
I am using a 1and1 server to house my php persons.php and the mysql database and I continue to get the following error:

Error: Not Found

Any suggestions?


<?php
print json_encode("Inserted");
?>

I even went as far as reducing my php code to the above and then tested it with the Insert new person button. I still receive ... Error: Not Found
 

rjanelli

Member
Licensed User
Longtime User
 

rjanelli

Member
Licensed User
Longtime User
Sorry my error. I have multiple servers and I was pointing to the wrong one. I spent two hours debugging code and it was a simple mistake. I hate that!
 

ciapw

Member
Licensed User
Hi. I have tried this.. but it didnt work. when i insert new person, it logs Back from Job:CountP, but there is nothing in my database. When i press the show person, it only logs <br>
The app stop when i want to count the person, it says json error. Where do I miss? thanks
my php code:
PHP:
<?php
$host = "127.0.0.1";
$db = "persons";
$user = "root";
$pw = "";

$con = mysqli_connect($host,$user,$pw) or die(mysql_error());
mysqli_select_db($db) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");
$action = $_GET["action"];
 
switch ($action)
{
    case "CountPersons":
        $q = mysqli_query($con,"SELECT * FROM persons");
        $count = mysqli_num_rows($q);
        print json_encode($count);
    break;
 
    Case "GetPersons":
        $q = mysqli_query($con,"SELECT name, age FROM persons");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
 
    case "InsertNewPerson":
        $name = $_GET["name"];
        $age = $_GET["age"];
        $q = mysqli_query($con,"INSERT INTO persons (name, age) VALUES ('$name', $age)");
        print json_encode("Inserted");
    break;
 
}
?>

my b4a code :
B4X:
#Region  Project Attributes
    #ApplicationLabel: Xampp php MySql
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private PersonsListview As ListView
    Private CountPersonsButton As Button
    Private GetPersonsButton As Button
    Private NameET As EditText
    Private AgeET As EditText
    Private InsertNewPersonButton As Button
    Private Label1 As Label
    Private Label2 As Label
    
    Private ServerIP As String
    
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    
    
    Activity.LoadLayout("1")
    Activity.Title ="Playing with php/MySql"
    
    CountPersonsButton.SetLayout(0,0,30%x,10%y)
    GetPersonsButton.SetLayout(33%x,0,30%x,10%y)
    InsertNewPersonButton.SetLayout (66%x,0,30%x,10%y)
    
    Label1.SetLayout(0,InsertNewPersonButton.Top+InsertNewPersonButton.Height+30dip, 30%x,10%y)
    Label2.SetLayout(0,Label1.Top+Label1.Height+5dip, 30%x,10%y)
        
    NameET.SetLayout(33%x,Label1.Top, 60%x,10%y)
    AgeET.SetLayout(33%x,Label2.Top, 60%x,10%y)
        
    PersonsListview.SetLayout(0,Label2.Top+Label2.Height+5dip, 100%x,10%y)
    PersonsListview.Height=100%y-PersonsListview.Height
    
    ServerIP="192.168.0.106" ' The ip address where you Xampp installation runs
    
End Sub

Sub CountPersonsButton_Click
    Dim CountPersons As HttpJob
    CountPersons.Initialize("CountP", Me)
    CountPersons.download2("http://" & ServerIP & "/tarmistor/myscript.php", Array As String ("action", "CountPersons"))
    
End Sub

Sub GetPersonsButton_Click
    Dim GetPersons As HttpJob
    GetPersons.Initialize("GetP", Me)
    GetPersons.download2("http://" & ServerIP & "/tarmistor/myscript.php", Array As String ("action", "GetPersons"))
End Sub

Sub InsertNewPersonButton_Click
    
    If NameET.Text.Length<2 Then
       Msgbox("Name is missing or to short", "Name")
       Return
    End If
    
    If AgeET.Text = "" Then
       Msgbox("Age is missing", "Age")
       Return
    End If
            
    Dim InsertNewPerson As HttpJob
    InsertNewPerson.Initialize("InsertNewP", Me)
    InsertNewPerson.download2("http://" & ServerIP & "/tarmistor/myscript.php", Array As String ("action", "InsertNewPerson", "name", NameET.Text, "age", AgeET.Text))
End Sub


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 Int
                
                ListOfPersons = parser.NextArray 'returns a list with maps
                
                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
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

The logs:
B4X:
Logger connected to:  asus Nexus 7
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Back from Job:InsertNewP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
Back from Job:CountP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
Back from Job:GetP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
Error occurred on line: 167 (Main)
java.lang.RuntimeException: JSON Array expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
    at XamppphpMysql.example.main._jobdone(main.java:571)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Back from Job:InsertNewP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Back from Job:InsertNewP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
Back from Job:CountP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
Back from Job:GetP
Response from server: <br />
<b>Warning</b>:  mysqli_select_db() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>C:\xampp\htdocs\tarmistor\myscript.php</b> on line <b>8</b><br />
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:571)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 

OliverA

Expert
Licensed User
Longtime User
mysqli_select_db($db) or die(mysqli_error());
Should have been
B4X:
mysqli_select_db($con, $db) or die(mysqli_error($con));
java.lang.RuntimeException: JSON Array expected.
PHP's die does not return JSON, yet your code assumes that it does. Also, PHP's die does not change the HTTP return code to a non-2xx value, therefore Job.Success evaluates to true. Why did CountP not throw a JSON error? Because you're using JSONParsers.NextValue. NextValue pretty much grabs anything. NextArray expects a valid JSON array, which in your case is not returned (you just die) and your app "blows up". If you don't want to get caught off guard by invalid JSON, you may want to check first if GetString returns JSON to begin with (an array should start with [ and end with ]).
 

Schakalaka

Active Member
Licensed User
Hello.
i'm using your example for my app.
i need to pick a single value from table. The table is for user fields and have many fields like (name, surname,,address, city, energy, etc.)

i would like pick the value "energy" from user with id "2"

So this is my code:

B4X:
Public Sub pickEnergy

    Dim DoUsersEnergy As HttpJob
    DoUsersEnergy.Initialize("", Me)
    DoUsersEnergy.download2("http://192.168.1.43/persons/persons.php", Array As String ("action", "GetUserEnergy","deviceid",functions.GetDeviceId))
    Wait For (DoUsersEnergy) JobDone(DoUsersEnergy As HttpJob)
    If DoUsersEnergy.Success Then
    Dim res As String
        res = DoUsersEnergy.GetString
        Log("Back from Job:" & DoUsersEnergy.JobName )
    Log("Response from server: " & res)
                Dim l As List
                l.Initialize   
    Dim parser As JSONParser
    parser.Initialize(res)
        l.Add(parser.NextValue)
    Log(l.Size)   
Log(l.Get(0))
        energy_Label.Text =  "Total energy: " & l.Get(0) & "/50"

    
Else
        ToastMessageShow("Error: " & DoUsersEnergy.ErrorMessage, True)
End If
    DoUsersEnergy.Release
        
End Sub

on php file, this is the code:

B4X:
    case "GetUserEnergy":
    $txtdeviceid =$_GET["deviceid"];
        $q = mysqli_query($con,"SELECT my_energy, FROM tbl_users WHERE my_deviceid = '$txtdeviceid'" );
        $rows = array();
        print json_encode($q);

     break;


All the code works, but return the value FALSE inside the editext...

How can i solve this?
 

le_toubib

Active Member
Licensed User
Longtime User
False usually means an error in the query
The is a coma in the query ..
Try this:
$q = mysqli_query($con,"SELECT my_energy FROM tbl_users WHERE my_deviceid = '$txtdeviceid'" );
 

Schakalaka

Active Member
Licensed User
no,..
without come, it crash and return this:

B4X:
Response from server: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

Error occurred on line: 315 (Main) (l.Add(parser.NextValue))
java.lang.RuntimeException: Simple value expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextValue(JSONParser.java:74)
    at com.clickand.claim.main$ResumableSub_pickEnergy.resume(main.java:704)
 

OliverA

Expert
Licensed User
Longtime User
The JSON returned is a map, not a simple value. You need to use NextObject, not NextValue. For anymore issues, please start a new thread.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…