Android Question Download with NET library

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I need to make a sub to download some files from my site. I am trying to use the Net library, with no success. It may be a syntax error or whatever else. The code is obtained by a new B4A project inserting the Upload example (changing to Download of course) in the Button1_Click. I simply get the unsuccessful log message. The file is located in the folder named Download, at first level in the site (i mean : www.mysite.it\Download\FileToDownload). I don't figure out which is the problem.. Thanks in advance.
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    'ftp was previously initialized And its event name was set To ftp:
    Dim ftp As FTP
    ftp.Initialize("ftp", "www.mysite.it", 21, "[email protected]", "password")
    Dim sf As Object = ftp.DownloadFile("PathOnServer\FileName.txt", True, File.DirDefaultExternal, "somefile.txt")
    Wait For (sf) ftp_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then
        Log("file was downloaded successfully")
    Else
        Log("Error downloading file")
        
    End If
End Sub
 

josejad

Expert
Licensed User
Longtime User
By the way, you probably don't have access to File.DirDefaultExternal, try with xui.defaultfolder
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
By the way, you probably don't have access to File.DirDefaultExternal, try with xui.defaultfolder
Thanks of hints, but no progress.
To be more precise (despite I don't know exacty whether what I do has influence or not) i did:
1) Added to manifest:
B4X:
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
)
2) Added RuntimePermissions and modified access to defaultExternal:
B4X:
    Dim sf As Object = ftp.DownloadFile("Download\xxx.txt", True, rp.GetSafeDirDefaultExternal(""), "somefile.txt")
Then I have decided to catch an unfiltered log, because maybe some expert can understand something. I have put two messages in the log, to isolate the actions that happen between pressing the Button1_Click and the Error message. Maybe this log gives useful indications.
 

Attachments

  • log.txt
    1.5 KB · Views: 162
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Moreover, about access to DirDefaultExternal, what I did works perfectly in other apps. I guess this is not the problem, but maybe I miss something here too..
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You might have tried this already, but in case you have not ...

Change your FTP download to put the file in xui.DefaultFolder. If this works then you will know that the problem is not with FTP (which I always find totally reliable) but somewhere else. Then you can sort out the external storage part of the operation.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
You might have tried this already, but in case you have not ...

Change your FTP download to put the file in xui.DefaultFolder. If this works then you will know that the problem is not with FTP (which I always find totally reliable) but somewhere else. Then you can sort out the external storage part of the operation.
Hi. I tried of course. No result. However, you say: "To put the file in xui.defaultfolder.."; while intellisense says "read only folder...", so, if I don't miss something, I cannot put anything there
BTW I am suspicious on one line of the log that I attached in previuos message: "getNetworkForDns: getNetId from enterpriseCtrl is netid 0". Maybe something related to DNS?
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Solved: the problem is the structure of my site. As a matter of fact, my provider manages 3 directories, due to backups. In practice we have:
1) a first level, which corrresponds to "mysite.it", in which we have (again) "mysite.it", "mysite_daily_backup" and "mysite_weekly_backup". This is my problem: the repeated name.
2) infact the folder "Download" is not internal to the first level "mySite.it" but to "MySite.it\MySite.it".
The problems is then resolved modifying the statement from:
B4X:
ftp.DownloadFile("\Download\FileToDownload.txt"...
to
B4X:
ftp.DownloadFile("\MySite.it\Download\FileToDownload.txt"...

Moreover, I don't know why everybody suggest to use xui.DefaulFolder. I got successful result with RuntimePermissions.GetSafeDirDefaultExternal("")

B4X:
Dim ftp As FTP
    ftp.Initialize("ftp", "www.mySite.it", 21, "[email protected]", "password")
    ftp.PassiveMode=True
    Dim sf As Object = ftp.DownloadFile("\mySite.it\Download\FileToDOwnload.txt", True, rp.GetSafeDirDefaultExternal(""), "somefile.txt")
    Wait For (sf) ftp_DownloadCompleted (ServerPath As String, Success As Boolean)

The resulting file appears in Android/Data/AppName/Files.

Sorry if trivial explanations. Not for gurus of course... Thanks to All.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't know why everybody suggest to use xui.DefaulFolder.
Because using the xui.DefaultFolder demonstrated, as you later confirmed, that the problem was in the "file download" part of the process, not in the "store to folder" part of the process.
 
Upvote 0
Top