Android Question OkHttpUtils2, one-on-one consultations.

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings,
I have the problem that I need to consult several web pages, however the code that I have first runs the entire for-next cycle and then passes the control to JobDone, I want is to execute the first element of the for-next cycle to pass the control To jobdone continue with the second element of the for-next
and so on ... i tried this but I did not get it to run one at a time ...


The idea is to have order the results obtained
Html (0) => Log (Job.GetString)
Html (1) => Log (Job.GetString)
Html (i) => Log (Job.GetString)

Another idea is not to use the job.Tag element



thank you.



B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
  
    For i=0 To 5
      job1.Download("https://www.pablo.com"&"/html("&i&")")
    Next
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job1"
                'print the result to the logs
                Log(Job.GetString)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 
Last edited:

PABLO2013

Well-Known Member
Licensed User
Longtime User
Thank you, but I'm sorry, I do not know what you mean, you would be so kind, to help me, thank you
 
Upvote 0

fireday

Member
Licensed User
Longtime User
B4X:
dim i as int

Sub Activity_Create(FirstTime As Boolean)

  i = -1
  call resumecode

End Sub

Sub JobDone(Link As String, Job As HttpJob)
  Log ("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
  Select Job.JobName
  Case "Job1"
  'print the result to the logs
  Log (Job.GetString)
  Job.Release
  Call resumecode
  exit sub
  End Select
  Else
  Log ("Error: " & Job.ErrorMessage)
  ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release
End Sub


Sub resumecode()

  If i < 5 Then
  Dim job1 As HttpJob
  job1.Initialize("Job1", Me)
  i = i + 1
  job1.Download("https://www.pablo.com"&"/html("&i&")")
  Exit Sub
  End If

  'do something

End Sub
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Explained:

If you fire 5 jobs at the same time they return in random sequences (e.g. 1 - 5 - 2 - 4 - 3). You can work with Job.Tag to identify each job. If you fire 100 Jobs or more the server can identify this as a attack so the last x jobs will maybe not return or the server has too much traffic then.

Best idea:

Fire one job. It returns to Job.Done. There you fire the next one until all jobs are done. See Firedays example. You can do it in many ways like create a List an add all of your jobs. If a job is finished, delete it from the list until all jobs are done.

Advantage:

Only one job is "running" at a time. The next is started when the previous one is done. If one isn't successful, it stays in the list and you can easily repeat it (e.g. the connection is bad). The server performance is much better and you can control everything in a nice and reliable way.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
job1.Download("https://www.pablo.com"&"/html("&i&")")
It is a mistake to reuse the same job.
You should dim and initialize a new Job each time.

Inside JobDone:
B4X:
Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
    job1.Download("https://www.pablo.com"&"/html("&i&")")
 
Upvote 0

fireday

Member
Licensed User
Longtime User
It is a mistake to reuse the same job.
Oh, sry!
U allright
i think about it, but when i write my post I got distracted - write code by automatical
I corrected my message - check it one more pls
 
Last edited:
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings
thank you Kmatle , fireday and DonManfred

I know it can be very obvious
but I do not quite understand it.
If they were kind enough to indicate a little orientation code,
a thousand thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See this example.

1. It downloads a list of images from my server
2. In JobDone a queue is generated with some random files from the imagelist...
3. the first download is started
4. In Jobdone (when the download is finished) the file is saved to disc and the job is removed from the queue.
5. The next queueitem is downloaded if there are more to download...
6. and so so and so on

See this example as an inspiration for your issue. You need to do something similar.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim allimages, todownload As List
    Private FilesToDownload As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    allimages.Initialize
    todownload.Initialize
    FilesToDownload.Clear
   
    Dim php As HttpJob
    php.Initialize("getimagesizes",Me)
    php.Download("http://snapshots.basic4android.de/imagesizes.php")
    ' Go ahead in JobDone to get the list
End Sub

Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success = False Then
        Log(Job.ErrorMessage)
    else If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("JobName: "&Job.JobName)
        If Job.JobName = "getimagesizes" Then
            'Log("result is: "&res)
            Dim parser As JSONParser
            parser.Initialize(res)
            Dim root As List = parser.NextArray
            allimages = root
            Log("Count images: "&allimages.Size)
            '
            ' We now get 50 Images from the list randomly and put them into a downloadqueue
            For i = 1 To 50
                Dim rand As Int
                rand = Rnd(0,allimages.Size)
                Dim m As Map = allimages.Get(rand)
                todownload.Add(m)
            Next
            Log("List of files to download contains "&todownload.Size&" items...")
            For Each colroot As Map In todownload
              Dim height As Int = colroot.Get("height")
              Dim width As Int = colroot.Get("width")
              Dim filename As String = colroot.Get("filename")
              Dim ext As String = colroot.Get("ext")
                Log(filename&" width="&width&", height="&height)
                FilesToDownload.AddTwoLines2(filename,"width="&width&", height="&height,colroot)
            Next
            If todownload.Size > 0 Then
                Dim m As Map = todownload.Get(0)
                Log("Download: "&m.Get("filename"))
                Dim j As HttpJob
                j.Initialize("DownloadImage",Me)
                j.Tag = m
                j.Download("http://snapshots.basic4android.de/"&m.Get("filename"))
            End If
        End If
        If Job.JobName = "DownloadImage" Then
            Dim m As Map = Job.Tag
      Dim OutStream As OutputStream
      Log("DownloadReady: "&Job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, m.Get("filename"), False)
      File.Copy2(Job.GetInputStream,OutStream) ' save the file
      OutStream.Close
      Log(m.Get("filename")&" is written to "&File.DirRootExternal)
            ' Remove the first job from queue
            If todownload.Size > 0 Then
                todownload.RemoveAt(0)
            End If
            ' Refill Listview
            FilesToDownload.Clear
            For Each colroot As Map In todownload
              Dim height As Int = colroot.Get("height")
              Dim width As Int = colroot.Get("width")
              Dim filename As String = colroot.Get("filename")
              Dim ext As String = colroot.Get("ext")
                Log(filename&" width="&width&", height="&height)
                FilesToDownload.AddTwoLines2(filename,"width="&width&", height="&height,colroot)
            Next

            ' Check if there are more files to download. If yes. Download them
            If todownload.Size > 0 Then
                Dim m As Map = todownload.Get(0)
                Dim j As HttpJob
                j.Initialize("DownloadImage",Me)
                j.Tag = m
                j.Download("http://snapshots.basic4android.de/"&m.Get("filename"))
            Else
                LogColor("======================================",Colors.Green)
                LogColor("===== ALL FILES FROM QUERE     =======",Colors.Green)
                LogColor("===== ARE FINISHED DOWNLOADING =======",Colors.Green)
                LogColor("======================================",Colors.Green)
            End If
           
                       
        End If
           
       
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

Attachments

  • getimagesizes.zip
    8 KB · Views: 279
Upvote 0
Top