Android Question HttpUtils2 remote server coding

jchal

Active Member
Licensed User
Longtime User
hi all
i thing today is the day of making samplecode on a remote server.......
i need some help for this task my sample code 1st is made within b4a the i try to put httputis2 and jason
i am going to post you my php scrip, my question is how to do the remote server and substitute thoes line of code , it is a 4 question game here is the b4a 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 MediaPlayer1,MediaPlayer2,MediaPlayer3  As MediaPlayer
    Type quizpar (First As String, Second As String ,third As String,forth As String, five As String,six As String )
    Dim timer1 As Timer
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Label1,LcorectAsw,Lscore As Label
   
    Private Button1,Button2,Button3,Button4,Bnext As Button
    Dim myscore As Int
    Dim L As List
    Dim wronganswer As Int
    Dim ImageView1, ImageView2, ImageView3 As ImageView

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    If FirstTime Then
        MediaPlayer1.Initialize( )
        MediaPlayer1.Load(File.DirAssets, "quizmusic.wav")
   
    End If
   
    L.Initialize
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("LQuestion")
   
    Button1.Color=Colors.Blue
    Button2.Color=Colors.Blue
    Button3.Color=Colors.Blue
    Button4.Color=Colors.Blue

   
    setupQuestion
'    LoadQuizQuestion
    MediaPlayer1.Play
   
   
  
   
    Looping_CheckedChange(True)
   
End Sub



Sub Looping_CheckedChange(Checked As Boolean)
    MediaPlayer1.Looping = Checked
End Sub


Sub Activity_Resume
    MediaPlayer1.Play
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub LoadQuizQuestion
    Dim jobadd As HttpJob
    jobadd.Initialize("quizquestion", Me)
    jobadd.Download("http://mydomain.com/quizq.php")
    ProgressDialogShow("Downloading Quiz Questions")
End Sub

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 = "quizquestion" Then
            Dim quizlist As List
            'Dim strOnline As String
            quizlist = parser.NextArray 'returns a list with maps
            For i = 0 To quizlist.Size - 1
                Dim m As Map
                m = quizlist.Get(i)
                Dim TL As quizpar 'TwoLines
               
                TL.First = m.Get("quizquestion")
                TL.Second = m.Get("quizanswer1")
                TL.third=m.Get ("quizanswer2")
                TL.forth=m.Get ("quizanswer3")
                TL.five=m.Get("quizanswer4")
                TL.six=m.Get("quizanswer5")
               
                Label1.Text= TL.First
                Button1.Text=TL.Second
                Button2.Text=TL.third
                Button3.Text=TL.forth
                Button4.Text=TL.five
                LcorectAsw.text=TL.six
                Exit
               
            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

Sub setupQuestion As Int
    Dim N As Int = (Rnd (0,48) +1) ' Numerber range 1-30
   
    Button1.Color=Colors.Blue
    Button2.Color=Colors.Blue
    Button3.Color=Colors.Blue
    Button4.Color=Colors.Blue
    Bnext.Color=Colors.Green
    Button1.Enabled=True
    Button2.Enabled=True
    Button3.Enabled=True
    Button4.Enabled=True
   
    If l.Size<41 Then
        Do While L.IndexOf(N)>-1
            N  = (Rnd (0,48) +1) ' Numerber range 1-30
        Loop
'************************************************************************
'******** this is the mess it should go to remote database************
        Select n
            Case 1
                n = 1
                Label1.Text= "q1"
                Button1.Text="1.A1"
                Button2.Text="2.A2"
                Button3.Text="3.A3"
                Button4.Text="4.A4"
                LcorectAsw.text=3
                Exit
            Case 2
                Label1.Text= "q2"
                Button1.Text="A1"             
                Button2.Text="2.A2"
                Button3.Text="3.A3"
                Button4.Text="4.A4"
                LcorectAsw.text=1
                n = 2
                Exit
            Case 3
                Label1.Text= "q3"               
               Button1.Text="A1"             
                Button2.Text="2.A2"
                Button3.Text="3.A3"
                Button4.Text="4.A4"
                LcorectAsw.text=3
                n = 3
                Exit
            .....................................
 .........................................................
            Case 48
                Label1.Text= "q48"               
               Button1.Text="A1"             
                Button2.Text="2.A2"
                Button3.Text="3.A3"
                Button4.Text="4.A4"
                LcorectAsw.text=1
                n = 48
                Exit
               
               
               
               
            End Select
   
'************************************************************************
'******** end of remote database************
   
       
    Else
        ' All number find, restart
        Msgbox( "Quiz End","myQuiz")
        'L.Clear
    End If
      
    L.Add(N)
    Return N
       
End Sub

Sub answerproc(answer As String)
   
   
    If answer=LcorectAsw.Text Then
       
        MediaPlayer2.Initialize( )
        MediaPlayer2.Load(File.DirAssets, "correct.wav")
        MediaPlayer2.Play
       
    '    Msgbox("Correct","QuizMania")
        If answer=1 Then Button1.Color=Colors.Yellow Else Button1.Color=Colors.Blue
        If answer=2 Then Button2.Color=Colors.Yellow Else Button2.Color=Colors.Blue
        If answer=3 Then Button3.Color=Colors.Yellow Else Button3.Color=Colors.Blue
        If answer=4 Then Button4.Color=Colors.Yellow Else Button4.Color=Colors.Blue
        myscore= myscore+10
        Lscore.text= myscore
       
        Bnext.Enabled=True
        Bnext.Visible=True
        'timer1.Initialize("Timer1_Tick", 1000)
        'timer1.Enabled = True
   
   
    Else
       
        MediaPlayer3.Initialize( )
        MediaPlayer3.Load(File.DirAssets, "wrong.wav")
        MediaPlayer3.Play
        wronganswer=wronganswer+1
        If wronganswer=1 Then ImageView3.Visible=False
        If wronganswer=2 Then ImageView2.Visible=False
        If wronganswer=3 Then ImageView1.Visible=False
        'Msgbox("False","QuizMania")
        If answer=1 Then Button1.Color=Colors.Red Else Button1.Color=Colors.Blue
        If answer=2 Then Button2.Color=Colors.Red Else Button2.Color=Colors.Blue
        If answer=3 Then Button3.Color=Colors.Red Else Button3.Color=Colors.Blue
        If answer=4 Then Button4.Color=Colors.Red Else Button4.Color=Colors.Blue
        Bnext.Enabled=True
        Bnext.Visible=True


    '    timer1.Initialize("Timer1_Tick", 1000)
    '    timer1.Enabled = True
       
            If wronganswer=3 Then
            Msgbox("GAME OVER - Please try again", "QuizMania")
                    'If answer=1 Then Button1.Color=Colors.Red Else Button1.Color=Colors.Blue
                    'If answer=2 Then Button2.Color=Colors.Red Else Button2.Color=Colors.Blue
                    'If answer=3 Then Button3.Color=Colors.Red Else Button3.Color=Colors.Blue
                    'If answer=4 Then Button4.Color=Colors.Red Else Button4.Color=Colors.Blue
                        wronganswer=0
                        myscore=0
            Lscore.text=  myscore
            ImageView1.Visible=True
            ImageView2.Visible=True
            ImageView3.Visible=True
            setupQuestion
            If IAd.Ready Then IAd.Show
            Else
                
            End If
            'Label1.Text="False"
       
   
       
    End If
   
End Sub


Sub Button1_Click()
    Button1.Enabled=False
    Button2.Enabled=False
    Button3.Enabled=False
    Button4.Enabled=False
    answerproc(1)
End Sub

Sub Button2_Click()
    Button1.Enabled=False
    Button2.Enabled=False
    Button3.Enabled=False
    Button4.Enabled=False
    answerproc(2)
End Sub

Sub Button3_Click()
    Button1.Enabled=False
    Button2.Enabled=False
    Button3.Enabled=False
    Button4.Enabled=False
    answerproc(3)
End Sub

Sub Button4_Click()
    Button1.Enabled=False
    Button2.Enabled=False
    Button3.Enabled=False
    Button4.Enabled=False
   
    answerproc(4)
End Sub

Sub Bnext_Click
    setupQuestion
    Bnext.Enabled=True
    Bnext.Visible=False
End Sub


there for i need some help with it how to do it??? i have manage to pass the parameter from php to the b4a but how i manage the case statement??????? i used to have , i need a procedure to but how?
below you will see my php it works but .... i am so close in doing this but .......i cant figure it out how to do it, any help i will be greatefull
php follows
PHP:
<?php
$host = "localhost";
$db = "mydb";
$user = "mydbuser";
$pw = "mypass";

$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 quizquestion, quizanswer1, quizanswer2, quizanswer3, quizanswer4, quizcorrectquestion FROM tbl_quizQuestions";
$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);
}
?>
 

jchal

Active Member
Licensed User
Longtime User
there are two difrent posts they are not the same, i have send you a pm
 
Last edited:
Upvote 0
Top