Android Tutorial Android FTP tutorial

Status
Not open for further replies.
Old and irrelevant tutorial. Follow this one instead: [B4X] Net library (FTP, SMTP, POP) with Wait For

This tutorial covers the FTP object which is part of the Net library.
The Net library is based on Apache Commons Net.

Android OS doesn't allow us, the developers, to block the main thread for more than 5 seconds. When the main thread is busy for too long and is not able to handle the user events the "Application not responding" dialog appears.
Therefore slow operations like network operations should be done in the background.
The FTP library is built in such a way. All of the methods return immediately. When a task completes an event is raised. In fact you can submit several tasks one after another. The FTP protocol supports a single task at a time so the tasks will be processed serially.

Using the FTP library is pretty simple.
The first step is to initialize the FTP object. If this is an Activity module then you should do it in Activity_Create when FirstTime is true.
For Service modules it should be initialized in Service_Create.
B4X:
Sub Process_Globals
    Dim FTP As FTP
End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        FTP.Initialize("FTP", "ftp.example.com", 21, "user", "password")
    End If
FTP.Initialize doesn't connect to the server. Connection will be established implicitly together with the next task.

Download

Downloading a file is done by calling DownloadFile with the remote file path and the local file path.
If you want to show the download progress then you should handle DownloadProgress event.
When download completes the DownloadCompleted event is raised. The ServerPath is passed as the first parameter to all events. You can use it to distinguish between different tasks. Make sure to check the Success parameter. If Success is False then you can find the exception message by calling LastException.

B4X:
    FTP.DownloadFile("/somefolder/files/1.zip", False, File.DirRootExternal, "1.zip")
   
Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)
End Sub
Upload

Uploading is similar to downloading.
B4X:
    FTP.UploadFile(File.DirRootExternal, "1.txt", True, "/somefolder/files/1.txt")

Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)
End Sub
List files and folders

FTP.List sends a request for the list of files and folders in a specific path.
The ListCompleted event is raised when the data is available.
You can use code similar to the following code to get more information on the entries:
B4X:
FTP.List("/")
...
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Log(ServerPath)
    If Success = False Then
        Log(LastException)
    Else
        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
        Next
    End If
End Sub
Delete
Delete is done by calling FTP.Delete with the full path. Again an event will be raised when the task completes (DeleteCompleted).

Closing the connection
You can close the connection by calling FTP.Close. Close will wait for the other tasks to complete and then will close the connection. This happens in the background.
FTP.CloseNow will immediately close the connection, failing the remaining tasks.

Some notes:
- At any given time there should be less than 15 waiting tasks. Otherwise you will get a RejectedExecutionException (this happens when the internal threads pool is exhausted).
- The order of the completed tasks may be different than the order of submission.
- The AsciiFile parameters sets the file transfer mode. If AsciiFile is true then every occurrence of an end of line character will be translated based on the server native end of line character. If your FTP server is Unix or Linux then the end of line character is the same as Android.
In most cases you can set AsciiFile to false.

The library is available for download here: http://www.b4x.com/forum/additional...92-new-net-library-android-ftp-smtp-pop3.html
 
Last edited:

androh

Member
Licensed User
Longtime User
How do you see that it is still running?

FTP_UploadProgress sub is running and updating progressbar in panel.
In device, network notification icon is blinking.
In ftp server, file size is growing.
I'm using Net library V1.2
 

androh

Member
Licensed User
Longtime User
I see it now. The data stream is not closed when you call CloseNow.

Please try the updated library attached.
It should close the data connection on the first time UploadProgress event fires (after calling CloseNow).

Thanks a lot. Now it's working :sign0060:
 

mujeeb74

Member
Licensed User
Longtime User
I have a problem when make upload for any file using NET Libraries.
I got a file size as 0KB in ftp server, Do I need to close ftp connection to complete uploading file.
 

Evadman

Member
Licensed User
Longtime User
I have a problem when make upload for any file using NET Libraries.
I got a file size as 0KB in ftp server, Do I need to close ftp connection to complete uploading file.

I had that problem when using the emulator, and when I ran the same code on a phone, I did not have that problem.
 

margret

Well-Known Member
Licensed User
Longtime User
I have an issue using B4A 2.02 and the FTP net lib. I am writing a Class for FTP that I will share once I am done. All works fine so far but the Total in FTP_DownloadProgress always returns -1. I want to update a ProgressBar with the download but the Total is missing. I saw in the first post of this thread the code used is If Total > 0 Then. Is this an item that is returned from some servers and not others, etc. The TotalDownloaded is fine, it's just the Total that always returns -1.

B4X:
Private Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    PB2.Visible = True
   PB2.BringToFront
   PB2.Progress = (100 / Total) * (TotalDownloaded)
End Sub
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
I have an issue using B4A 2.02 and the FTP net lib. I am writing a Class for FTP that I will share once I am done. All works fine so far but the Total in FTP_DownloadProgress always returns -1. I want to update a ProgressBar with the download but the Total is missing. I saw in the first post of this thread the code used is If Total > 0 Then. Is this an item that is returned from some servers and not others, etc. The TotalDownloaded is fine, it's just the Total that always returns -1.

B4X:
Private Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    PB2.Visible = True
    PB2.BringToFront
    PB2.Progress = (100 / Total) * (TotalDownloaded)
End Sub

Without knowing the exact cause, I would suspect it is something similar to what we often see while downloading from some servers using our pc-based browsers, where the total size of the download is not shown (shows a 'unknown' time left). So I would guess that -1 is some sort of response error.
 

mc73

Well-Known Member
Licensed User
Longtime User
Idont know how can i add the .jar file needed pelase publish an examplee

Grab both xml and jar files of the library and place them in the 'libraries' folder of your b4a installation folder. Then when in the b4a ide, look at the right panel, choose libs, and then select the desired library.
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hi,
using library, I've got this log error:

(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)

What's wrong? :BangHead:
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
While I'm working, doing the same things, I get that error on the log.

(ErrnoException) libcore.io.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)(ErrnoException) libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
.......................

and the list go on with the same error
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Are you using a real device? On mobile network or wifi?

Have you tried changing to passive mode?

Yes, I'm using a real device with wifi. I also changed to passive mode, but nothing
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
This error hints to some network problem.

When does this error happen? Is the download successful?

This error happen when I download. But not at the first time, after about 5 minutes that I don't use the tablet
 
Status
Not open for further replies.
Top