Android Tutorial For beginners: How to communicate with a server using httputils2 (Part 1: php)

KMatle

Expert
Licensed User
This tutorial is deprecated. You can still use it to understand the basic communication. A newer one can be found here: https://www.b4x.com/android/forum/threads/updated-mysql-php-example.96790/#post-610554 It's for B4J but the code can be copied and used for your B4A app.



Hi guys,

this is a tutorial for beginners. My intention is to show how the different parts work together.



Part 1: A little php is needed

Did you ever use Google? Of course you did. And at that moment you communicated with a server. With b4a & httputils2 it's exact that simple, too.

Let's see what Google does when you type "b4a" in that search bar and you press return. Take a look at the address bar:

www.google.de/search?q=b4a&oq=b4a&aqs=chrome.0.69i59j69i60l2.1088j0j8&sourceid=chrome&es_sm=122&ie=UTF-8

Google is calling a script called "search" with some parameters. Each parameter has a name and a value, terminated by a &

And this is exactly what we will use, too to get our data from our app to a server and back.

Take a look at Erels example to communicate with servers:

http://www.basic4ppc.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/

Take a look at this:

B4X:
'Send a POST request
  job2.Initialize("Job2", Me)
  job2.PostString("http://www.basic4ppc.com/print.php", "first key=first value&key2=value2")
As you can see there is a (post)string very similar to what we've seen, Google does. And yes, it IS the same thing.

And now click on this url:

http://www.basic4ppc.com/print.php?first key=first value&key2=value2

As you see, there is no big difference between sending this string from an app or typing it in your browser. The php script "print.php" receives the data and does something with it. In this case it just prints the received data.

What do I need to use my own scripts?

You will need a running php installation (later we want to use MySql and you will need that, too). Most people get a package/website from a hoster including webspace (I pay about 8€ a month). I think there are some free hosters, too. Browse for it.

Of course you can have a local installation but it is LOCAL and not easy to handle.

How does a php script look like?

Here you go:

B4X:
<?

$p1 = $_GET["p1"];
$p2 = $_GET["p2"];

print ("I've received $p1 and $p2");

?>
Following the examples above, this script is called in the same way. In you browser you would type:

B4X:
http://yourdomain.com/examples/myscript?p1=php&p2=MySql
On the given website there is a folder called "examples". In it is the script above (named "myscript").

The output would be:

I've received php and MySql
From an app you would call it:

B4X:
job2.Initialize("Job2", Me)
  job2.PostString("http://www.yourdomain.com/examples/myscript.php", "p1=php&p2=MySql")

What does the script do?

First it "gets" the parameters (in our example we know them by name: p1 and p2), The "print" gives back an answer to the caller (browser or app).

In Erels example you can see this sub:

B4X:
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
      Select Job.JobName
        Case "Job1", "Job2"
            'print the result to the logs
            Log(Job.GetString)
        Case "Job3"
            'show the downloaded image
            Activity.SetBackgroundImage(Job.GetBitmap)
      End Select
  Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release
End Sub
After SENDING the data to the script (p1 & p2) the app gets back to "JobDone" after the script has finished automatically and RECEIVES the answer by Job.GetString.

That's all!

Erel uses 3 Jobs to handle different paramters and functions. All VERY easy!

You will find the php documentation in the www: http://de1.php.net/manual/en/

In the next part I will show how to use php with a MySql database with parameters via php script including a sample how to build a user registration by mail.
 
Last edited:

jayel

Active Member
Licensed User

KMatle

Expert
Licensed User
Change you code to:

B4X:
Dim job2As HttpJob
    job2.Initialize("Job2", Me)
    job2.Download2("http://test.elftalmanager.be/index.php", _
              Array As String("p1", "Php", "p2", "MySql"))
In JobDone:

B4X:
Sub JobDone(Job As HttpJob)
  
    If Job.Success Then
        Dim Res As String
        Res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & Res)
     
        ...
It is better to put the php in a folder. The name INDEX.php could be critical due to name restrictions. So rename it to something else.
 
Last edited:

achtrade

Active Member
Licensed User
How can I use Download2 with this code ?

B4X:
Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("http://www.xxx.com/myfolder/myfile.php", Query)
End Sub
I tried this, but it didn't work:

B4X:
Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.Download2("http://www.xxx.com/myfolder/myfile.php",ArrayAsString( Query))
End Sub
 

SimonElck

Member
Licensed User
I think there is a mistake in the php,
I think it should read:
<?

$p1 = $_POST["p1"];
$p2 = $_POST["p2"];

print ("I've received $p1 and $p2");

?>
 

SimonElck

Member
Licensed User
The original posting never did a Download2, it did a PostString
therefor I am not wrong, believe me, I tried it.
 

DonManfred

Expert
Licensed User
How to show website to webview using your example?
You should create a new thread in the questionsforum for your issue. It has nothing to do with a webview...


You get the data and how you present it in your app is totally up to you. If you want to show in a webview then create a html-page which you then load in a webview.
 
Top