Read and display infos from a php page.

Monkey Targets Forums/Android/Read and display infos from a php page.

hub(Posted 2013) [#1]
i am a newbie with monkey.
From my php5/mysql online planning (http://www.hub73.com/hopital/) i want display the day info on my android mobile phone :

Today : Lundi 7 RH repos.

So i write a php code to display only this on the site...

http://www.hub73.com/hopital/today.php

So now how to read and display this with my monkey program ?

many thanks !


Midimaster(Posted 2013) [#2]
Use the HttpRequest()function! In the bananas folders you will find a sample code.


hub(Posted 2013) [#3]
Import mojo

Import brl.httprequest

Class MyApp Extends App Implements IOnHttpRequestComplete

    Field get_req:HttpRequest,post_req:HttpRequest
    
    Method OnHttpRequestComplete:Void( req:HttpRequest )
    
        If req=get_req
            Print "Http GET complete!"
        Else
            Print "Http POST complete!"
        Endif

        Print "Status="+req.Status()
        Print "ResponseText="+req.ResponseText()
        
    End
    
    Method OnCreate()
    
        get_req=New HttpRequest( "GET","http://hub73.com/hopital/today.php",Self )
        get_req.Send
        
        post_req=New HttpRequest( "POST","http://hub73.com/hopital/today.php",Self )
        post_req.Send "Hello World!"
            
        SetUpdateRate 60
    End
    
    Method OnUpdate()

        If KeyHit( KEY_CLOSE ) Error ""

        UpdateAsyncEvents
    End

    Method OnRender()
    
        Cls
        
        DrawText "Http GET bytes received="+get_req.BytesReceived(),0,0
        DrawText "Http POST bytes received="+post_req.BytesReceived(),0,12

    End 
End

Function Main()

    New MyApp

End


many thanks
ResponseText return nothing in my case ?!? any idea ?


Midimaster(Posted 2013) [#4]
1.
If you do not plan to send datas to your server, it is enough to use only one method: Use the "GET" method only.

2.
Did the original code sample in bananas received something?

3.
For which target do you build? HTML5 only allows to receive datas from the server where the HTML5 app was started. Try to communicate with XAMPP (localhost) first.


hub(Posted 2013) [#5]
1) just read the text displayed.
2) yes. Original code works fine.
3) html5. What is xampp ?

why http://posttestserver.com and not http://www.posttestserver.com ?

thanks.


hub(Posted 2013) [#6]
is there another way to do this ?
i can create a text file on the server. Next is there a way to read it as 'Local Fichier:TStream = OpenFile (CTE_URL_FICHIER_SCORES$)' (blitzmax) ?


Midimaster(Posted 2013) [#7]
Banana sample code

How can the original banana code work perfect? If you start it on your computer as HTML5 target it must produce an error!



This how it would work:

You build your app "myapp" on the computer. Then you have to transfer the "myapp.build"-folder with a FTP application to your server directory

http://www.hub73.com/hopital/...

In the end it looks like:

http://hub73.com/hopital/myapp.build/

and then call the app in a browser window:

http://www.hub73.com/hopital/myapp.build/html5/MonkeyGame.html

Now this app would work fine with all files on this server. Now this http-request would work:

get_req=New HttpRequest( "GET","http://www.hub73.com/hopital/today.php",Self )




What is XAMPP?

because this procedure is very inconvenient , you better download a free webserver, which runs on your windows system. During the development you use this server instead of the hub73.

XAMPP can run php scripts as well as html pages. HttpRequest() works perfect with it. After installation you copy your server-folder into the

C:\xampp\htdocs\

In the end it looks like:

C:\xampp\htdocs\hopital\...

Now you can start XAMPP and there you can start "Apache". Now you can call the php-script in your browser with...

localhost/hopital/today.php

then you copy also the build folder to xampp:

C:\xampp\htdocs\hopital\myapp.build\

and you can call the app in the browser with:

localhost/hopital/myapp.build/html5/MonkeyGame.html

In the code you replace this adress:

get_req=New HttpRequest( "GET","http://localhost/hopital/today.php",Self )