Click Twice for HttpUtils2

dewif

Member
Licensed User
Longtime User
Hi everyone, I need help quickly please.
I can't click button login once to login. I have to click it twice.
Maybe there's something wrong with my code. Please take a look.
I though the problem is my for..next code run before poststring is finished.
i put the JobDone sub in a service module.
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
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 job1  As HttpJob
   Dim tb_user As EditText
   Dim tb_pass As EditText
   Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Activity.Title = "SI SMK Yamasi"
   Activity.LoadLayout("lay_login")
   'Msgbox(Activity.Width,"Info")
   job1.Initialize("job1",serv_var)
   ImageView1.Top = Activity.Height - 400
   
End Sub

Sub Activity_Resume
ImageView1.Top = Activity.Height - 400
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub posting 
   job1.PostString(mod_var.server_url,"Select tb_user.username, tb_user.pass from tb_user where username = '" & tb_user.text & "' and pass = '" & tb_pass.Text & "'")
End Sub
Sub bt_login_Click
   
   CallSubDelayed(Me,"posting")
   If job1.Success = True Then
      For i = 0 To serv_var.rows.Size - 1
         Dim m As Map
         m = serv_var.rows.get(i)
         If tb_user.Text = m.Get("username") AND tb_pass.Text = m.Get("pass") Then
            StartActivity(act_main_menu)
            Activity.Finish
         End If
         'Msgbox("Username : " & m.Get("username") & ", Password : " & m.Get("pass"),"Success")
      Next
   End If

End Sub
Thanks before.
 

dewif

Member
Licensed User
Longtime User
I'm sorry, i'm a very newbee in this b4a thing. what handling did you mean?
I put the JobDone event in a service module. If i put CallSubDelay2(serv_var,"JobDone","job1") before the loop For..Next in the bt_login_click, it returns Force Close.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't completely understand your code.
This condition will always be false on the first run:
B4X:
If job1.Success = True Then
The request will only be sent after this sub is executed. I recommend you to intercept JobDone in the activity and handle the login there.

Note that you should never specify screen dimensions without 'dip' or percentage:
B4X:
ImageView1.Top = Activity.Height - 400dip 'added dip
 
Upvote 0

dewif

Member
Licensed User
Longtime User
Sorry, but I can't put login handler in the JobDone sub because i want make it more dynamic so I can get another value with another post. This is my code for a while :
Main Activity :
B4X:
Sub bt_login_Click
   serv_var.act = "Main"
   serv_var.theSub = "cek"
   job1.PostString(mod_var.server_url,"Select id_staff, username, pass from tb_user where username = '" & tb_user.text & "' and pass = '" & tb_pass.Text & "'")
End Sub
Sub cek
   If job1.Success Then
      For i = 0 To serv_var.rows.Size - 1
         Dim m As Map
         m = serv_var.rows.get(i)
         If tb_user.Text = m.Get("username") AND tb_pass.Text = m.Get("pass") Then
            mod_var.id_staff = m.Get("id_staff")
            StartActivity(act_main_menu)
            Activity.Finish
         End If
         'Msgbox("Username : " & m.Get("username") & ", Password : " & m.Get("pass"),"Success")
      Next
   End If
End Sub

serv_var Service
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim response As String
   Dim parser As JSONParser
   Dim rows As List
   Dim act As String
   Dim theSub As String
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub JobDone(Job As HttpJob)
   If Job.Success = True Then
      response = Job.GetString
      parser.Initialize(response)
      'Dim rows As List
      rows = parser.NextArray
      If rows.Size <= 0 Then
         Msgbox("Data yang Anda inginkan tidak ada.","Error")
      End If
   Else
      Msgbox(Job.ErrorMessage,"Error")   
   End If
   If IsPaused(act) = False Then 
      CallSubDelayed(act,theSub)
   End If
   Job.Release
End Sub

And thanks for the advice about dimension. :sign0188:
 
Upvote 0

dewif

Member
Licensed User
Longtime User
it's from the first one. It's just i modify it so I can get the value after parsing and bring it to main activity.
I've trace it. After Job.Relese, the step back to Activity Create then to Activity Resume.
 
Upvote 0
Top