Android Question Download Multiple Text Files From Dropbox Using Dropbox Sync API With a Service

Mahares

Well Known Member
Licensed User
I use a service to download 6 text files from Dropbox to my tablet using Erel’s Dropbox lib 1.25 and Dropbox API Sync lib 2.0. The files are uploaded manually from my PC to Dropbox server. The files range from 1KB to 300 KB each. The file names are always the same, but the sizes change. The service runs every 3 minutes for the sake of this test. I only get the new files every other service run. It seems like it is not syncing with the new files in Dropbox server. When I check Dropbox server, the new files always appear almost immediately after I upload the 6 files to it from my PC. Could you please find my problem in the attached code and project. I did not include the files because they are simply any text files.
Thank you

'BELOW SERVICE CODE:
B4X:
Sub Process_Globals
    Dim sNotify As Notification
End Sub
Sub Service_Create
   sNotify.Initialize
   sNotify.Icon = "icon"
   sNotify.SetInfo("HHE","Auto Download DropBox Files",Main)
   sNotify.Sound = True
   sNotify.Notify(1) 
   Service.StartForeground(1,sNotify)
End Sub
Sub Service_Start (StartingIntent As Intent)
    StartActivity(Main)
End Sub
'BELOW ACTIVITY MOST IMPORTANT CODE:
B4X:
 If FirstTime Then
      Manager.Initialize(keyDBX, secretDBX, "Manager")
     Manager.LinkAccount  'must link to account
   End If
   Manager.AutoSync =True  'per new Erel lib 1.25

   DBFilePath = File.DirRootExternal & "/HHE"  'subfolder called HHE
   TenderID="DG1"
   MyListtwo.Initialize

End Sub

Sub Activity_Resume
    Manager.LinkAccount  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Manager_AccountReady (Success As Boolean)
   MyListtwo.Initialize  
   Log("Account Ready: " & Success)
   If Success =True Then
      'Export is a Dropbox subfolder where all 6 files are downloaded from Dropbox
     'The 6 files are all text files ranging from 1 kB to 300 KB max each.
     'The 6 File names: DG1_Base_Comments.tab, DG1_Base_Consumables.tab, DG1_Base_Settings.tab,
     'DG1_Base_Goals.tab, DG1_Base_Wells.tab, DG1_Daily.csv
      For Each FileInfo As DbxFileInfo In Manager.ListFiles("/Export")       
        If FileInfo.Name.StartsWith(TenderID) OR FileInfo.Name.StartsWith("Base_")  Then
           MyFile=FileInfo.Name
            MyListtwo.Add(MyFile)
        End If
      Next
   End If
   
   If Success=True Then 
      CallSub(Me,"Download")  'start downloadt    
     Dim p As Period   'for testing service run every few minutes
     p.Minutes = 3  
     Dim NextSchedule As Long = DateUtils.AddPeriod(DateTime.Now, p)
     StartServiceAt(svcDownload,NextSchedule,True)
      Activity.Finish
   End If
End Sub

Sub Manager_DownloadCompleted (Success As Boolean, LocalDir As String, LocalFileName As String)
   Log("Download Completed: " & Success)
End Sub

Sub Download
     For i=0 To MyListtwo.Size-1
        MyFile=MyListtwo.Get(i)
        GetFile
    Next
End Sub

Sub GetFile
       Manager.DownloadFile("/Export", MyFile, DBFilePath, MyFile)
        Log(MyFile)
       ToastMessageShow(MyFile,False)
End Sub
 

Attachments

Mahares

Well Known Member
Licensed User
Moving the Activity.Finish to this sub does not solve the problem I am having:
B4X:
Sub Manager_DownloadCompleted (Success As Boolean, LocalDir As String, LocalFileName As String)
   Log("Download Completed: " & Success)
    Activity.Finish  'moved it here 12/21/14
End Sub
I have been struggling with this problem for over a year without a good solution. Either my application has a fundamental flaw, in which case I posted the complete project for someone to help or the Dropbox lib is not reliable downloading the most recent files every time. Maybe there a better way to download files from Dropbox than using Dropbox Sync API.
Thanks
 

Mahares

Well Known Member
Licensed User
How do you wait until all files are downloaded other than what I posted. I am not following what you are recommending.
 

Mahares

Well Known Member
Licensed User
I have followed Erel's suggestions to raise the downloadcomplete after each file, but the problem still persists: The most recent files only download every other service schedule. Here is my code responsible for the download. I hope someone can find this nagging problem I have been having for the last 12 months. If not please propose a better way to download multiple files from Dropbox:
B4X:
Sub Manager_AccountReady (Success As Boolean)
   MyListtwo.Initialize  
   Log("Account Ready: " & Success)
   If Success =True Then
'   Manager.AutoSync =True  'per new Erel lib 1.25
      'Export is a Dropbox subfolder where all 6 files are downloaded from Dropbox
     'The 6 files are all text files ranging from 1 kB to 200 KB max each.
     'The 6 File names: DG1_Base_Comments.tab, DG1_Base_Consumables.tab, DG1_Base_Settings.tab,
     'DG1_Base_Goals.tab, DG1_Base_Wells.tab, DG1_Daily.csv
      For Each FileInfo As DbxFileInfo In Manager.ListFiles("/Export")       
        If FileInfo.Name.StartsWith(TenderID) OR FileInfo.Name.StartsWith("Base_")  Then
           MyFile=FileInfo.Name
            MyListtwo.Add(MyFile)
        End If
      Next
     Counter=MyListtwo.Size  '12/21/14
   End If  
   If Success=True Then 
      CallSub(Me,"Download")  'start downloadt    
     Dim p As Period   'for testing service run every few minutes
     p.Minutes = 3  
     Dim NextSchedule As Long = DateUtils.AddPeriod(DateTime.Now, p)
     StartServiceAt(svcDownload,NextSchedule,True)
   End If
End Sub

Sub Manager_DownloadCompleted (Success As Boolean, LocalDir As String, LocalFileName As String)
   Log("Download Completed: " & Success)
   Counter=Counter -1 
   If Counter = 1 Then
        Activity.Finish
    End If   
End Sub

Sub Download
     For i=0 To MyListtwo.Size-1
        MyFile=MyListtwo.Get(i)
        GetFile
    Next
End Sub

Sub GetFile
       Manager.DownloadFile("/Export", MyFile, DBFilePath, MyFile)
        Counter=Counter
        Log(MyFile)
       ToastMessageShow(MyFile,False)
End Sub
 

Erel

Administrator
Staff member
Licensed User
My guess is that it takes some time to the internal Dropbox service to update the data. The purpose of Dropbox Sync api is to allow developers to work with a local cache without the need to deal with synchronization issues. If synchronization is critical then Dropbox (at least Sync) might not be the best solution.
Why not use a standard FTP server?
 

Mahares

Well Known Member
Licensed User
Why not use a standard FTP server?
Thank you Erel for your valiant help. This is for a customer that does not like FTP for their fear of security. They insist on Dropbox. I hope you have taken a final look at my code in the preceding post to make sure there is nothing faulty that is causing it to skip every other service. I am extremely surprised no one else complained about this or attempted to give their opinion when I know that many are using Dropbox. Sometimes I wonder if many are reluctant to respond to posts because they are intimidated by the rank of someone in the forum asking a question when that someone like me has very limited knowledge of the subject.
 
Top