Android Example B4A and PHP REST

Songwut K.

Licensed User
Thanks @Erel for free version that I plan to buy but donate instead.

I'll share my B4A knowledge in Thailand, your great product must popular, later.

PHP is well known for web developer, it cheap resource and easy to learn in Thailand.
Backend system easy manage by PHP & MySQL but till now, 2020 no one share how to use with B4A.

This is my first app that I try to learn from many post in webboard.
I don't have best practice about b4a and php, so this is my share that can help myself to remind.


My concept is get text from B4A texbox r_hospital, after click Button1 data will send to my server with data in textbox r_hospital.

some part of B4A Code

B4A Code:
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
    ' Gen by B4A Designer

    Private Label1 As Label
    Private Button1 As Button
    Private hospital As Label
    Private php As Label
    Private server As Label
    Private Button2 As Button
    Private Label5 As Label
    Private Label6 As Label
    Private Label7 As Label

    Private r_hospital As EditText  ' input box
    Private t_hospital As Label     ' pull data from server to show
    Private r_php As Label            ' pull data from server to show
    Private r_agent As Label        ' pull data from server to show

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Activity1") ' Link from Designer
End Sub

Sub Button1_Click
    ProgressDialogShow("Fetching data : "&r_hospital.Text)  ' Show message when click button
    Dim Job As HttpJob
    Job.Initialize("dsrest", Me)   ' Init HttpJob
    Job.PostString("https://(myweb)/demo.php","hospital="&r_hospital.text ) ' POST to url with text from box
End Sub

Sub Button2_Click
    ExitApplication  ' click to quit
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Dim res As String
        res = Job.GetString                  ' Result from php store to res
        Log("Response from server: " & res)  ' Show all of result from server to log
        Dim parser As JSONParser
        parser.Initialize(res)               ' Parse JSON to variable
        Dim m As Map
        m = parser.NextObject                 ' Parse object to Map
        If m.Size = 0 Then
            t_hospital.Text="Empty"             ' If empty data show empty message
            t_hospital.Text= m.Get("hospital")   ' get data from server to label
            r_php.Text= m.Get("php")
            r_agent.Text= m.Get("agent")
        End If
        Log(Job.ErrorMessage)                    ' Error detect
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
End Sub
you must check these library

Last edited:

Songwut K.

Licensed User
PHP Code [ demo.php ]
PHP Code:

// define  sample data  array for return
$data= ['hospital'=>'phrae',

// save data from mobile to logfile in server
// please change file permission  (linux) to 777

        $file=fopen("data.log","w");   // log file, same path of php
        fwrite($file,file_get_contents('php://input'));  // all request data save to log file
        fclose($file); // close file when write complete

if($_POST["hospital"]!=""){ $data["hospital"]=$_POST["hospital"];}  // if android had fill data in textbox, store to $data["hospital"]
$data["php"]=$_SERVER['SERVER_SOFTWARE'];          // store server info to $data["php"]
$data["agent"]=$_SERVER['HTTP_USER_AGENT'];        // store client android browser agent to $data["agent"]

$json=json_encode($data);                                        // encode $data array to json

header("Access-Control-Allow-Origin: *");                                // set header prepare for send back to android
header("Content-Type: application/json; charset=UTF-8");        // set type of data to json and use utf-8 charset
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");

echo $json;                                                                //  Reply data to android
Highlight lines is important part.
Line 12 - 14 is for understand input from android that store value to log file name "data.log", you can delete these lines.

if no input data will send from variable "$data" that can varies from server environment.

the result of code is in json format

{"hospital":"phrae","php":"Apache\/2.4.6 (CentOS) OpenSSL\/1.0.2k-fips PHP\/7.3.13","server":"apache","agent":"Mozilla\/5.0 (Windows NT 6.1; Win64; x64; rv:73.0) Gecko\/20100101 Firefox\/73.0"}
Firefox browser extension show in pretty format.

Last edited:


Active Member
Licensed User
Great Job with your Tutorial. But I just want to draw your attention to some few things.

I change file icon.png with my icon made by photoshop 128x128 pixel save to png format.
Do not store anyfile in folder except icon.png, the only one.
Try and make your icons 512x512. This will look nicer and can be used for uploading to play store.
Also, another way to set App icon is to click on
project->Choose icon.

set icon.png

Name of my App show below the icon. I change by edit file .b4a
2. Changing Application name to show under the icon can also be done right in the code under project attributes in the main module, no need to edit the .b4a file itself


Well-Known Member
Licensed User
You dont need thise headers. For allow method post just check if the $_POST var is empty or not before processing.

Allow origine, max age and content type is applicable for web platform.


Well-Known Member
Licensed User
There are some examples connect B4A to MySQL using PHP by Kmatle
I also posted a project for demo