Android Question Simultaneous HttpJobs?

geoffschultz

Member
Licensed User
Longtime User
In my Activity_Create code I submit 2 HttpJob requests, but I only see 1 job request complete. The request which completes is random. Based upon logs that I create on the server, both requests are returning json data. Can I only have 1 request outstanding at once? I'm targeting 4.4.2 and I'm testing this on an emulator running 4.4.2.

B4X:
    Dim GetVol As HttpJob
    GetVol.Initialize("VolunteerList", Me)
    GetVol.Download2(url, Array As String("barcode", "VL", "state", 0, "json", ""))
  
    Dim GetItems As HttpJob
    GetItems.Initialize("ItemList", Me)
    GetItems.Download2(url, Array As String("barcode", "IL", "state", 0, "json", ""))

Here's part of the HttpJob code:

B4X:
Sub JobDone(Job As HttpJob)
   ProgressDialogHide
   If Job.Success Then
    Dim res As String
     res = Job.GetString
     Log("Back from Job:" & Job.JobName )
     Log("Response from server: *" & res & "*")

Here's what I see on the IDE logs:

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **

Back from Job:VolunteerList
Response from server:  [json data]
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Dim GetVol As HttpJob
GetVol.Initialize("VolunteerList", Me)
GetVol.Download2(url, Array As String("barcode", "VL", "state", 0, "json", ""))

Dim GetCust As HttpJob
GetCust.Initialize("ItemList", Me)
GetCust.Download2(url, Array As String("barcode", "IL", "state", 0, "json", ""))

The code looks good. should work

please upload your project (export as zip) and we´ll help finding the problem.

Your code seems to work only if the job success = true... But what if success is not true?

Try this jobdone sub to find out more and/or have a look at the example. It uses two jobs at once

B4X:
Sub JobDone(job As HttpJob)
    If job.Success Then
        Dim res As String
    res = Job.GetString
    Log("Back from Job:" & Job.JobName )
    Log("Response from server: *" & res & "*")
    Else
    Log("Back from Job:" & Job.JobName )
        Log("Error: " & job.ErrorMessage)
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub
 

Attachments

  • simultanoushttpjobs.zip
    6.6 KB · Views: 124
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
I believe that the best route is to call the second job after the success of the previous one.

something like
B4X:
Dim GetVol As HttpJob
    GetVol.Initialize("VolunteerList", Me)
    GetVol.Download2(url, Array As String("barcode", "VL", "state", 0, "json", ""))

and then

B4X:
Sub JobDone(Job As HttpJob)
   ProgressDialogHide
   If Job.Success Then
' do what you want and
'Launch next job

Dim GetItems As HttpJob
GetItems.Initialize("ItemList", Me)
GetItems.Download2(url, ArrayAsString("barcode", "IL", "state", 0, "json", ""))


of course that you need to analyse and parse whatever you want to do acording to what you want to achieve and organize the code in the correct way. This is just to ilustrate the idea.
 
Upvote 0

geoffschultz

Member
Licensed User
Longtime User
I restarted the emulator and the problem went away. I'm new to B4A, so perhaps I need to get used to trying that. Thanks for the help!

-- Geoff
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You can start as many jobs as you like.

As DonManfred has written, it depends if they have to be called in sequence or not (f.e. if you check one thing with the first job and the 2nd job needs this information).

A nice thing is to tag the jobs:

B4X:
Dim GetImage As HttpJob
          GetImage.Initialize("GetImage", Me)
          GetImage.Tag=filen 'filename
          GetImage.Download("http://www.xxxx.com/" & filen)

Here you can put any value and you can check if all jobs are back (if needed).

In one App I use it because I load all files from a folder (images). Every image has a corresponding text file. So I put the filename (which I retrieved as a list before) in it.

When a job comes back I always know which file I got and which format it has (.jpg or .txt).
 
Upvote 0
Top