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

Discussion in 'Tutorials & Examples' started by KMatle, Jun 29, 2014.

  1. KMatle

    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: 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:

    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:

    Take a look at this:

    'Send a POST request
      job2.Initialize("Job2", Me)
    """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: 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:


    $p1 = $_GET[
    $p2 = $_GET[

    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:

    On the given website there is a folder called "examples". In it is the script above (named "myscript").

    The output would be:

    From an app you would call it:

    job2.Initialize("Job2", Me)

    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:

    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
    Case "Job3"
    'show the downloaded image
    End Select
    Log("Error: " & Job.ErrorMessage)
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    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:

    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: Sep 4, 2018
    Watchkido1, Procesor, Isac and 23 others like this.
  2. jayel

    jayel Active Member Licensed User

  3. KMatle

    KMatle Expert Licensed User

    Change you code to:

    Dim job2As HttpJob
    "Job2", Me)
    "", _
    Array As String("p1""Php""p2""MySql"))
    In JobDone:

    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: Feb 20, 2015
  4. achtrade

    achtrade Active Member Licensed User

    How can I use Download2 with this code ?

    Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
        job.Initialize(JobName, Me)
    "", Query)
    End Sub
    I tried this, but it didn't work:

    Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
        job.Initialize(JobName, Me)
    "",ArrayAsString( Query))
    End Sub
  5. SimonElck

    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");

  6. DonManfred

    DonManfred Expert Licensed User

    You´re wrong.
    Download2 is doing an GET request. So getting the Parameters out of $_GET is the correct way.

    Your code will only work if using a POST request.
  7. SimonElck

    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.
  8. Jeboiii

    Jeboiii Member Licensed User

    How to show website to webview using your example? --> webview1.Loadurl("?")

    job2.Initialize("Job2", Me)
    job2.PostString("", "first key=first value&key2=value2")
  9. DonManfred

    DonManfred Expert Licensed User

    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.
    mangojack likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice