service modul download

tudorf

Active Member
Licensed User
Longtime User
Hi Erel

Klaus says me you have written the service modul.

The download wonderfully functions. Unfortunately, only with a file.
However, I have the problem that I would like to load any number of files
automatically. I put to myself a dynamic Array or a list before with
sourcefile and targetfile. I wanted to fill this and then hand over to the
service module. Because I the construction of the service module do not
understand me do not know like me this should do. Does this go generally?

Martin (Tudorf)
 

tudorf

Active Member
Licensed User
Longtime User
Sorry. I can´t find a sample for download many files from the web.

I have test the sampe HttpUtils2. It shows ownly a green adroid man. Where can I download files?

What is " Array As String("first key", "first value :)", "second key", "value 2"))
in the sample?

I wan´t to use a dynamic array or a list with source and target.
{"http://img.geocaching.com/cache/large/source1.jpg","target1.jpg};
{"http://img.geocaching.com/cache/large/source2.jpg","target2.jpg};
{"http://img.geocaching.com/cache/large/source3.jpg","target3.jpg};
{"http://img.geocaching.com/cache/large/source4.jpg","target4.jpg}

Martin
 
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
I can download 1 JPG. No I have problems to download more JPGs.

I have generated a list with source and target. But I have no access to the Strings. Is the defination wrong?

Sub Globals
Dim list1 As List
Dim akt_pos As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim job3 As HttpJob

list1.Initialize
' Soure , target
list1.AddAll(Array As String("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg","GC1YRK0-Test1"))
list1.AddAll(Array As String("http://img.geocaching.com/cache/large/2e335b83-ddbe-4370-b2c9-ba42de733093.jpg","GC3GWDT-Test2"))
list1.AddAll(Array As String("http://img.geocaching.com/cache/large/9b6a99dc-4cf1-4f41-93fd-47841444f2d9.jpg","GC3BM6H-Test3"))
list1.AddAll(Array As String("http://img.geocaching.com/cache/large/ef0af76f-6285-4458-88a6-5e4385ab45ce.jpg","GC3BM6H-Test4"))



job3.Initialize("Job3", Me)
' job3.Download("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg")

For i = 0 To list1.Size - 1
akt_pos = i
job3.Download(list1.get(akt_pos,1))
Next i

End Sub

Sub JobDone (Job As HttpJob)
If Job.Success = True Then
Activity.SetBackgroundImage(Job.GetBitmap)
Dim out As OutputStream
' out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery","Test.jpg",False )
out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery",list1.Get(akt_pos,2),False )
File.Copy2(Job.GetInputStream, out)
out.Close
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub

Martin
 
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
Sorry. I understand what you mean. My english is not so good.

I have a dynamic array. For example I want to download 1 picture or 100 or 200 picture. I understand as I must create 100 oder 200 downloadjobs. I must change the name of the picture.

I have attached my testprogram.
Martin
 

Attachments

  • httputils2_Tudorf.zip
    8.3 KB · Views: 165
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
I have an error "too many parameters" in the code
j.Download(list1.get(akt_pos,1))

The list1 has a array with 2 parts.
list1.AddAll(Array As String("Source1.jpg","Target1.jpg"))
Is the access list1.get(akt_pos,1) and list1.get(akt_pos,2) correct?

For my understanding. I start the job in the sub Activity_create. The jobs ends and the i can save the jpg in the sub JobDone. Does the next job starts after saving or does all jobs start to the same time.

How can I assign the save_name to the file?

I have define a global varibale akt_pos for both Sub. So I must add the code
akt_sub = i ?

Martin
 
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
are you sure. I have the error
Error description: 'as' expected.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim job3 As HttpJob

   list1.Initialize
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg","GC1YRK0-Test1.jpg"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/2e335b83-ddbe-4370-b2c9-ba42de733093.jpg","GC3GWDT-Test2.jpg"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/9b6a99dc-4cf1-4f41-93fd-47841444f2d9.jpg","GC3BM6H-Test3.jpg"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/ef0af76f-6285-4458-88a6-5e4385ab45ce.jpg","GC3BM6H-Test4.jpg"))
   

   job3.Initialize("Job3", Me)
'   job3.Download("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg")

   For i = 0 To list1.Size - 1
      Dim j As HttpJob
      akt_pos = i
      j.Initialize("j", Me) 
      Dim str() As String = list1.get(akt_pos)
      j.Download(str(0))
   Next

End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Activity.SetBackgroundImage(Job.GetBitmap)
      Dim out As OutputStream
'      out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery","Test.jpg",False )

      Dim str() As String = list1.get(akt_pos)

      out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery",str(1),False )
      File.Copy2(Job.GetInputStream, out)
      out.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub
Martin
 
Last edited:
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
I have change the programm. It doesn´t work correct. From my 4 files was 1 file saved. The preview is a other as the file himself. I can´t find a sampe in the forum with saving more as 1 file. Can you send me a link ?

B4X:
Sub Globals
   Dim list1 As List
   Dim akt_pos As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim job3 As HttpJob

   list1.Initialize
'   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg","GC1YRK0-Test1.jpg"))
'   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/2e335b83-ddbe-4370-b2c9-ba42de733093.jpg","GC3GWDT-Test2.jpg"))
'   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/9b6a99dc-4cf1-4f41-93fd-47841444f2d9.jpg","GC3BM6H-Test3.jpg"))
'   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/ef0af76f-6285-4458-88a6-5e4385ab45ce.jpg","GC3BM6H-Test4.jpg"))
   
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/[email protected]"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/[email protected]"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/[email protected]"))
   list1.AddAll(Array As String("http://img.geocaching.com/cache/large/[email protected]"))

   job3.Initialize("Job3", Me)
'   job3.Download("http://img.geocaching.com/cache/large/5d53aaf9-f92c-4691-9141-130054b72f75.jpg")

   For i = 0 To list1.Size - 1
      Dim j As HttpJob
      akt_pos = i
      j.Initialize("j", Me) 
      Dim str1 As String 
      str1 = list1.get(akt_pos)
      
      Dim zw_zeile As String
      Dim bis As Int
      bis = str1.IndexOf("@")
      zw_zeile = str1.SubString2(0,bis)   
      
      j.Download(zw_zeile)
   Next

End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Activity.SetBackgroundImage(Job.GetBitmap)
      Dim out As OutputStream
'      out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery","Test.jpg",False )

      Dim str1 As String 
      str1 = list1.get(akt_pos)
      Dim zw_zeile As String
      Dim von As Int
      von = str1.IndexOf("@")
      zw_zeile = str1.SubString2(von + 1 , str1.Length)   
      
Msgbox(zw_zeile,"Save "&akt_pos)      
      
      out = File.OpenOutput(File.DirRootExternal&"/Test_Mystery",zw_zeile,False )
      File.Copy2(Job.GetInputStream, out)
      out.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub
Martin
 
Upvote 0

tudorf

Active Member
Licensed User
Longtime User
I can´t find the file an my samsung. How can I change the name or directory? I thing I must copy the file with file.copy2?
I am very confus
Martin
 
Upvote 0

Taha

Member
Licensed User
Longtime User
You are correct. After the resource is downloaded you should use File.Copy2 to copy it.

Hi, Erel!

It looks like Job is similar to BackgroundWorker in VS.

Is there any way to make a main module wait till the file is really downloaded? I did in many ways including above - nothing helps. I use datepicker to change the date, it sends request to http site and downloads text file I fill the table from. However, I need to change the date picker 2-3 times and only then the data in my table refreshes.

EDIT: It looks like I found the reason of the problem - I would highly recommend to put everything related to downloaded file(s) into JobDone sub. This sub is not quite friendly with other subs.

Thanks! :BangHead:
 
Last edited:
Upvote 0
Top