Android Question HttpUtils2 remote server steaming

jchal

Active Member
Licensed User
Longtime User
hi
i have this b4a code which i thougth it is not nice and that it would be better if i put a remote sever database the code will be shorter and better but i am stuk in the way i must thing to do this, php code works
here is the b4a code and the php code
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim mp As MediaPlayerStream
End Sub


Sub Globals
    Private BannerAd As AdView
    Private IAd As InterstitialAd
        'Dim ImageView1 As ImageView
    Dim Label1 As Label
    'Dim Panel1 As Panel
    'Dim Panel2 As Panel
    Dim Button1 As Button
    Dim State As Int
    Dim Url As String
    Dim ListView1 As ListView
    Dim Station As String
    Dim UrlDefiner As Int
    Dim Bitmap1 As Bitmap
    Dim Bitmap2 As Bitmap
    Dim Bitmap3 As Bitmap
    Dim Bitmap4 As Bitmap
    Dim Bitmap5 As Bitmap
  
   
    Dim Button1 As Button
    Dim Button2 As Button
    Dim Button3 As Button
    'Dim INPhoneNumber As String
    'Dim INText As String
    Bitmap1.Initialize(File.DirAssets, "1.jpg")
    Bitmap2.Initialize(File.DirAssets, "2.jpg")
    Bitmap3.Initialize(File.DirAssets, "3.jpg")
    Bitmap4.Initialize(File.DirAssets, "4.jpg")
    Bitmap5.Initialize(File.DirAssets, "5.jpg")  
       
    Dim Stream(36) As String
    Stream(1) = "http://examplestream/;listen.pls" ' RB
    Stream(2) = "http://examplestream:8081/listen.mp3" ' ambient
    Stream(3) = "http://examplestream/;listen.pls" ' classic mix
    Stream(4) = "http://examplestream:8081/listen.mp3" ' eighties
    Stream(5) = "http://examplestream:8104/stream" ' current top 40
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("Layout1")
    ListView1.TwoLinesAndBitmap.Label.TextSize= "14"
       
    ListView1.AddTwoLinesAndBitmap("radio descreption1", "", Bitmap1)
    ListView1.AddTwoLinesAndBitmap("radio descreption2", "", Bitmap2)
    ListView1.AddTwoLinesAndBitmap("radio descreption3", "", Bitmap3)
    ListView1.AddTwoLinesAndBitmap("radio descreption4", "", Bitmap4)
    ListView1.AddTwoLinesAndBitmap("radio descreption5", "", Bitmap5)
      
    ListView1.AddSingleLine(" ")
    If FirstTime Then
        mp.Initialize("mp")
    End If
    Label1.Text = "Click a Station to Listen"

End Sub

Sub mp_StreamReady
    Log("starts playing")
   
    mp.Play
    Label1.Text = "Playing - " & Station
End Sub
Sub mp_StreamError (ErrorCode As String, ExtraData As Int)
    Log("Error: " & ErrorCode & ", " & ExtraData)
    ToastMessageShow("Error: " & ErrorCode & ", " & ExtraData, True)
End Sub
Sub mp_StreamBuffer(Percentage As Int)
    Log(Percentage)
End Sub
Sub Stop_Click
    Log("stop playing")
    mp.Stop
    State = 2
    Label1.Text = "Click a Station to Listen"
   
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Station = Value
    UrlDefiner = Position + 1
    If State = 1 Then Stop_Click
    Url = Stream(UrlDefiner)
    mp.Load(Url)
    State = 1
    Label1.Text = "Buffering"
End Sub
Sub Button1_Click
    If State = 1 Then Stop_Click
End Sub

and i try to convert it to a database with 3 fealds
rdstream,rdname,rdimage
my php code is
PHP:
<?php
$host = "localhost";
$db = "mydbname";
$user = "mydbuser";
$pw = "mydbuserpassword";

$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
    
$sql = "SELECT rdstream, rdname, rdimage FROM tbl_radiostations";
$res = mysql_query($sql);

if (mysql_errno()) {
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'\n';
    echo mysql_error();
}
else
{
    $rows = array();
    while($r = mysql_fetch_assoc($res)) {
        $rows[] = $r;
    }
    print json_encode($rows);
}
?>

what i try to do if i dont make it clear is to use remote database where i store the radio station secription, stream,and image in stead the way i have it now, i will appreciate your help
 

DonManfred

Expert
Licensed User
Longtime User
What is the URL of the php-script? Use okhttputils and do a Download with this url.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
i have pm the url link
i use httpUtils2 ver 2.01 but i can change this to okhttputils but i dont uderstand how to do
do a Download with this url.
as my logic is not correct in b4a
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("url of your php script")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim root As List = jp.NextArray
        For Each colroot As Map In root
            Log("RD()")
            Dim rdname As String = colroot.Get("rdname")
            Dim rdstream As String = colroot.Get("rdstream")
            Dim rdimage As String = colroot.Get("rdimage")
            Log("rdname="&rdname)
            Log("rdstream="&rdstream)
            Log("rdimage="&rdimage)
        Next
     End If
    j.Release
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
i also tryed this part of code in order to do the conversion but........ i canot figure how to do the rest

B4X:
Type radiolist (First As String, Second As String ,third As String )

B4X:
Sub LoadjobaddList
    Dim jobadd As HttpJob
    jobadd.Initialize("radios", Me)
    jobadd.Download("the url i send you")
    ProgressDialogShow("Downloading radios")
End Sub
and
B4X:
Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "radios" Then
            Dim radioslist As List
            'Dim strOnline As String
            radioslist = parser.NextArray 'returns a list with maps
            For i = 0 To radioslist.Size - 1
                Dim m As Map
                m = jradioslist.Get(i)
                Dim TL As addlist 'TwoLines
            '    If m.Get("online") = "Y" Then
            '        strOnline = " (Online)"
            '    Else
            '        strOnline = ""
            '    End If
                TL.First = m.Get("rdstream")
                TL.Second = m.Get("rdname")
                TL.third=m.Get ("rdimage")
               
             
              Label1.Text= TL.First
                Label2.Text=TL.Second
                         Label3.Text=TL.forth
  '******************************************************************************            
  ' up to here i mange to pass the parameters from the database to b4a but what next???????????
   'how to pass the parameter to the rest ???
'****************************************************************************************
            Next
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
but how to make
B4X:
Bitmap1.Initialize(File.DirAssets, "1.jpg")
    Bitmap2.Initialize(File.DirAssets, "2.jpg")
    Bitmap3.Initialize(File.DirAssets, "3.jpg")
    Bitmap4.Initialize(File.DirAssets, "4.jpg")
    Bitmap5.Initialize(File.DirAssets, "5.jpg")
 
    Dim Stream(36) As String
    Stream(1) = "http://examplestream/;listen.pls" ' RB
    Stream(2) = "http://examplestream:8081/listen.mp3" ' ambient
    Stream(3) = "http://examplestream/;listen.pls" ' classic mix
    Stream(4) = "http://examplestream:8081/listen.mp3" ' eighties
    Stream(5) = "http://examplestream:8104/stream" ' current top 40
and
B4X:
ListView1.AddTwoLinesAndBitmap("radio descreption1", "", Bitmap1)
    ListView1.AddTwoLinesAndBitmap("radio descreption2", "", Bitmap2)
    ListView1.AddTwoLinesAndBitmap("radio descreption3", "", Bitmap3)
    ListView1.AddTwoLinesAndBitmap("radio descreption4", "", Bitmap4)
    ListView1.AddTwoLinesAndBitmap("radio descreption5", "", Bitmap5)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("radiolist.php")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim root As List = jp.NextArray
        For Each colroot As Map In root
            Log("RD()")
            Dim rdname As String = colroot.Get("rdname")
            Dim rdstream As String = colroot.Get("rdstream")
            Dim rdimage As String = colroot.Get("rdimage")
            Dim radio As radiolist
            radio.Initialize
            radio.First = rdname
            radio.Second = rdstream
            radio.third = rdimage
            Log (radio)
            Log("rdname="&rdname)
            Log("rdstream="&rdstream)
            Log("rdimage="&rdimage)
        Next
     End If
    j.Release
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
don my problem is how to pass the parameters
like
is it correct to say for example

B4X:
Bitmap1.Initialize(File.DirAssets, rdimage)
and
B4X:
Stream(1) = rdstream  ' RB
B4X:
ListView1.AddTwoLinesAndBitmap(rdname, "", Bitmap1)
to do this type to do this type of substitioution will it work? but how?
 
Last edited:
Upvote 0
Top