B4J Tutorial [B4x] Exchange Data via OkHttpUtils and php (or other platforms) via lists, maps, arrays, etc.

Discussion in 'B4J Tutorials' started by KMatle, Sep 21, 2019.

  1. KMatle

    KMatle Expert Licensed User

    I made 5 examples how to exchange data between B4x apps and php. I use the POSTSTRING method as it is simple, secure (get-methods will be logged with the data autmatically on the server, post-methods not), strings can be formatted very easy (JSON) and en-/decrypted (not included but see my other examples or the php documentation).

    You can use these basics for other "platforms", too like asp.net or even any other sw to use a "standard" exchange format which uses "one simple string" which is JSON formatted.

    What you need:

    B4J :cool:
    XAMPP (as a good test system which comes whith apache, php, MySQL and other components). You can use any other, too. The examples just use apache and php. I use it since years because it comes with a "one click install" and has all important modules (like OpenSSL for encryption).

    https://www.apachefriends.org/de/index.html

    1. Install it (c:/xampp ist the standard folder which I use)
    2. Copy the 5 php scripts (you find them inside the B4J project under files) to xamp/htdocs/phpexamples (create 'phpexamples' first)
    3. Start the apache service via XAMPP control center

    Run it with administrator rights. Apache listens to ports 80 and 443. If some other app uses these ports, just close it. Check your firewall if you run into problems

    xampp.JPG

    4. In the B4J app edit these lines to fit your environment. Hint: For every app your create which uses php create an own folder under /htdocs

    Code:
    'the ip address of the machine where apache runs (can be the same as where B4J runs)
        Public Servername As String = "http://192.168.178.23"
        
    'the path where the php files are (example 1-5) under htdocs
        Public ScriptFolder As String = "/phpexamples/"
    5. Run the B4J app. It executes all the 5 examples:

    #1: Call the first php script with no input. It returns just a simple string
    #2: Same as #1 but it returns an simple array (list/array in B4x or other platforms)
    #3: returns a associative array which is a map in B4x with key -> value pairs
    #4: returns a list with arrays = list with maps
    #5: send a map to the script and it returns the map's contents

    6. The B4J app checks what the scripts return and logs e.g. if it is a map, a list, a list with maps or just a string for learning purposes

    7. All lists/maps/arrays are JSON encoded/decode to/from a string which is VERY easy to use.
     

    Attached Files:

    cyiwin, miker2069, Myr0n and 4 others like this.
Loading...
  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