Hi there.
Looking for some help from the experts.
Have been playing around with Services and the data download stuff as created by Erel, and after having made lots of progress learning about this Android and B4A stuff, I have come up against a problem.
My service – WebAccessService – is responsible for calling a specified website, downloading the data in a JSON Map, then saving the data into a local database table using a modified version of DBUtils. It’s very much based on Erel’s code to access a site which runs a PHP script against a MySQL database, and get the result as a JSON data structure.
The main activity has a loop which loops through a list of tables, and uses the table names to set up WebAccessService before starting the service to allow it to download the data for the specified table. This is supposed to allow the user to switch out of the program and the downloads will continue in the background. This works fine.
The mods to DBUtils that I have done allow a callback from DBUtils so that I can monitor the progress of the download using a progress bar.
Initially, the index into the list of tables was controlled by a variable which increments when I press a button view on my activity.
This was so that I could see what was happening as I developed the code. It works fine like this – I press my Download button and the program iterates through each table and shows me a progress bar on the ScrollView UI for each table as it does so. I even have a “Downloaded x of y” thing going on, so the user is not left wondering what is going on.
However, I want ALL the tables to be iterated through automatically one by one rather than having to press a button. So, to that end I changed the button press code into a for/next loop, and put a
DO WHIILE WebAccessService.JOB_STATUS=WORKING
DoEvents
LOOP
into the for/next loop, so that each call to StartService(WebAccessService) would get a chance to complete and update the UI before moving to the next table.
Here’s the problem. As a result of using the FOR/NEXT loop and the DO WHILE LOOP, the WebAccessService inside the for/next loop no longer even gets created! The program gets stuck in the DO WHILE LOOP, but through various LOG calls, I can see that the call to StartService(WebAccessService) does not appear to be working.
If I switch my code back to use the manually incremented index, it works fine! The scroll bars work, everything works, calls to the WebService work - all fine.
If I put the loop back in - the WebService does not get a look-in.
A shortened version of the main loop is shown below. The WebAccessService is almost identical to the Services example that Erel has posted already.
This code won't work as it stands - it just gives the bare bones as I've ripped big chunks of UI stuff out.
Any thoughts as to how I can make my main loop wait until the service sends back JOB_DONE before moving to the next table would be gratefully received.
JMB
*********************************************************
Sub Process_Globals
Dim NumberOfTables As Int :NumberOfTables=22
End Sub
Sub Globals
Dim ButtonStartDownLoad As Button
Dim scvMain As ScrollView
Dim Counter As Int :Counter = 0
[UI VARIABLES]
End Sub
Sub Activity_Create(FirstTime As Boolean)
[SET UP UI ETC ETC – this all works]
FillScrollView ‘this is the View which has a list of progress bars
‘for each table
End Sub
‘Here’s the button which manually increments the counter for the tables
Sub ButtonStartDownLoad_Click
ButtonStartDownLoad.Enabled=False
Do_Download
Counter= Counter +1
If Counter =22 Then
Counter =0
End If
End Sub
Sub Do_Download
Dim Panel1 As Panel
Dim View1 As View
Dim i As Int
‘i= Counter ‘i is the index into the list of tables
For i=0 To NumberOfTables-1
[SOME MORE UI STUFF TO POINT TO THE RIGHT UI – WORKS OK]
'Here's where we set up the call to WebAccessService to get the table stuff
ProgressDialogShow("Communicating with Server...")
[make the desired table – this DROPS it first, then creates it]
CoreDataAccess.Create_Table(Main.SQL1,i)
CoreDataAccess.TableName_For_RequestID(i).TableName)
[SET UP FOR THE WEB ACCESS SERVICE]
WebAccessService.URL="***.php?reqtype=GET_ALL_DATA&reqvar=" & CoreDataAccess.TableName_For_RequestID(i).TableName
WebAccessService.FileTarget="page.html"
WebAccessService.RequestID=i
WebAccessService.SQLHandler=SQL1
[THE CALL!]
StartService(WebAccessService)
[HERE’s the loop that kills it]
Do While WebAccessService.JobStatus=WebAccessService.STATUS_WORKING
DoEvents
Loop
Next
End Sub
Sub FinishDownload
ProgressDialogHide
If WebAccessService.DoneSuccessfully Then
If WebAccessService.Callback_TableIsEmpty = False Then
[update ui]
Else
[update ui]
End If
End If
ButtonStartDownLoad.Enabled=True
WebAccessService.JobStatus=WebAccessService.STATUS_NONE
End Sub
Sub Activity_Resume
If ButtonStartDownLoad.Enabled=False AND WebAccessService.JobStatus=WebAccessService.STATUS_DONE Then
FinishDownload
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub FillScrollView
[Sets up the ui - WORKS]
End Sub
Looking for some help from the experts.
Have been playing around with Services and the data download stuff as created by Erel, and after having made lots of progress learning about this Android and B4A stuff, I have come up against a problem.
My service – WebAccessService – is responsible for calling a specified website, downloading the data in a JSON Map, then saving the data into a local database table using a modified version of DBUtils. It’s very much based on Erel’s code to access a site which runs a PHP script against a MySQL database, and get the result as a JSON data structure.
The main activity has a loop which loops through a list of tables, and uses the table names to set up WebAccessService before starting the service to allow it to download the data for the specified table. This is supposed to allow the user to switch out of the program and the downloads will continue in the background. This works fine.
The mods to DBUtils that I have done allow a callback from DBUtils so that I can monitor the progress of the download using a progress bar.
Initially, the index into the list of tables was controlled by a variable which increments when I press a button view on my activity.
This was so that I could see what was happening as I developed the code. It works fine like this – I press my Download button and the program iterates through each table and shows me a progress bar on the ScrollView UI for each table as it does so. I even have a “Downloaded x of y” thing going on, so the user is not left wondering what is going on.
However, I want ALL the tables to be iterated through automatically one by one rather than having to press a button. So, to that end I changed the button press code into a for/next loop, and put a
DO WHIILE WebAccessService.JOB_STATUS=WORKING
DoEvents
LOOP
into the for/next loop, so that each call to StartService(WebAccessService) would get a chance to complete and update the UI before moving to the next table.
Here’s the problem. As a result of using the FOR/NEXT loop and the DO WHILE LOOP, the WebAccessService inside the for/next loop no longer even gets created! The program gets stuck in the DO WHILE LOOP, but through various LOG calls, I can see that the call to StartService(WebAccessService) does not appear to be working.
If I switch my code back to use the manually incremented index, it works fine! The scroll bars work, everything works, calls to the WebService work - all fine.
If I put the loop back in - the WebService does not get a look-in.
A shortened version of the main loop is shown below. The WebAccessService is almost identical to the Services example that Erel has posted already.
This code won't work as it stands - it just gives the bare bones as I've ripped big chunks of UI stuff out.
Any thoughts as to how I can make my main loop wait until the service sends back JOB_DONE before moving to the next table would be gratefully received.
JMB
*********************************************************
Sub Process_Globals
Dim NumberOfTables As Int :NumberOfTables=22
End Sub
Sub Globals
Dim ButtonStartDownLoad As Button
Dim scvMain As ScrollView
Dim Counter As Int :Counter = 0
[UI VARIABLES]
End Sub
Sub Activity_Create(FirstTime As Boolean)
[SET UP UI ETC ETC – this all works]
FillScrollView ‘this is the View which has a list of progress bars
‘for each table
End Sub
‘Here’s the button which manually increments the counter for the tables
Sub ButtonStartDownLoad_Click
ButtonStartDownLoad.Enabled=False
Do_Download
Counter= Counter +1
If Counter =22 Then
Counter =0
End If
End Sub
Sub Do_Download
Dim Panel1 As Panel
Dim View1 As View
Dim i As Int
‘i= Counter ‘i is the index into the list of tables
For i=0 To NumberOfTables-1
[SOME MORE UI STUFF TO POINT TO THE RIGHT UI – WORKS OK]
'Here's where we set up the call to WebAccessService to get the table stuff
ProgressDialogShow("Communicating with Server...")
[make the desired table – this DROPS it first, then creates it]
CoreDataAccess.Create_Table(Main.SQL1,i)
CoreDataAccess.TableName_For_RequestID(i).TableName)
[SET UP FOR THE WEB ACCESS SERVICE]
WebAccessService.URL="***.php?reqtype=GET_ALL_DATA&reqvar=" & CoreDataAccess.TableName_For_RequestID(i).TableName
WebAccessService.FileTarget="page.html"
WebAccessService.RequestID=i
WebAccessService.SQLHandler=SQL1
[THE CALL!]
StartService(WebAccessService)
[HERE’s the loop that kills it]
Do While WebAccessService.JobStatus=WebAccessService.STATUS_WORKING
DoEvents
Loop
Next
End Sub
Sub FinishDownload
ProgressDialogHide
If WebAccessService.DoneSuccessfully Then
If WebAccessService.Callback_TableIsEmpty = False Then
[update ui]
Else
[update ui]
End If
End If
ButtonStartDownLoad.Enabled=True
WebAccessService.JobStatus=WebAccessService.STATUS_NONE
End Sub
Sub Activity_Resume
If ButtonStartDownLoad.Enabled=False AND WebAccessService.JobStatus=WebAccessService.STATUS_DONE Then
FinishDownload
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub FillScrollView
[Sets up the ui - WORKS]
End Sub