Android Question Standard method for receiving and using information from host

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello guys ?

I want to get a json data from the host and use it in different parts of the program.

I know how to request, save and analyze this data.:cool:

But !!!!

What is the standard way to do this in B4X and B4xPages?



This is the method I have chosen,Because I need the data from the beginning



1-I made a module from -> b4a -> class module -> standard class -> (AppDataModule)
2-I introduced it in "Main Activity"
3-I will send the request and if it is successful save it in the list
4-I will wait a little (sleep 500)
5-I control the value of the list if it has value, it means it is successful and I will start the program
6-Then I use the filled list everywhere in the program
B4X:
Sub Process_Globals
    Public AppDataModule1 As AppDataModule
    Public ListDataModule1 As List
End Sub

Sub Activity_Create(FirstTime As Boolean)

    AppDataModule1.Initialize
   AppDataModule1.GetAndSetData

sleep(500)

if ListDataModule1.size > 0 then

    Dim B4XPagesManager1 As B4XPagesManager
    B4XPagesManager1.Initialize(Activity)

else

MsgboxAsync("Error receiving data ,exit and try later", "Error ")

endif

End Sub



3-in "AppDataModule"

B4X:
Sub Class_Globals

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    GetAndSetData
End Sub

Sub GetAndSetData


    ProgressDialogShow2("please wait ...",True)

    Dim remotehostjob As HttpJob
    remotehostjob.Initialize("remote_host",Me)
    remotehostjob.Download("http://www.xxxxxxxxxx.php?action=yyy")
    Wait For (remotehostjob) JobDone(remotehostjob As HttpJob)
    If remotehostjob.Success Then

        Dim res As String = remotehostjob.GetString
        LogColor("Back from JobName:" & remotehostjob.JobName ,0xFFD2691E)
        LogColor("Response from server: " & res ,0xFFD2691E)
        '

        Dim parser As JSONParser
        parser.Initialize(res)
        Dim map1 As Map = parser.NextObject         'is a map
        Main.ListDataModule1  = map1.Get("xyz")             'is a list


                
   
    
    Else
        Main.ListDataModule1 .Clear
    End If
    remotehostjob.Release

  ProgressDialogHide

End Sub
 
Last edited:
Top