Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List Windows Mobile Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android FTP tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2011, 08:37 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,807
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android FTP tutorial

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.
Code:
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.

Code:
    FTP.DownloadFile("/somefolder/files/1.zip"FalseFile.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.
Code:
    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:
Code:
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.basic4ppc.com/forum/addit...smtp-pop3.html
Reply With Quote
  #2 (permalink)  
Old 07-28-2011, 08:42 PM
Newbie
 
Join Date: Jun 2011
Posts: 9
Default

I need an example file please!

I copy the indications but dont work me
Reply With Quote
  #3 (permalink)  
Old 07-29-2011, 04:45 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,807
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

What error do you get?
Reply With Quote
  #4 (permalink)  
Old 07-29-2011, 11:01 AM
AscySoft's Avatar
Knows the basics
 
Join Date: Jul 2011
Posts: 179
Default

Does this library knows sftp protocol? Sorry, this is my first day with android/b4a
__________________
for me, English is a foreign language, so be patient
Reply With Quote
  #5 (permalink)  
Old 07-29-2011, 12:13 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,807
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

No, only FTP is supported.
Reply With Quote
  #6 (permalink)  
Old 07-29-2011, 12:54 PM
AscySoft's Avatar
Knows the basics
 
Join Date: Jul 2011
Posts: 179
Default

Then I must encode/zip files manually for strong security reason then upload them using ftp! Coud you point to a solution/link that sound anything like that? I mean, zip with password/encode files etc
__________________
for me, English is a foreign language, so be patient
Reply With Quote
  #7 (permalink)  
Old 07-29-2011, 02:51 PM
Newbie
 
Join Date: Jun 2011
Posts: 9
Default

Quote:
Originally Posted by Erel View Post
What error do you get?
I'd like to sync through ftp but not responding.
The code i have is the following:


Code:
Sub Process_Globals
...
    
Dim FTP As FTP
End Sub

Code:
Sub Activity_Create(FirstTime As Boolean)
    Activity.AddMenuItem2(
"Sincronizar","update2",LoadBitmap(File.DirAssets,"refresh.png"))
    
If FirstTime Then
        
FTP.Initialize("FTP""ftp.xxx"21"user""pass")
    
End If
End Sub


Code:
Sub Update2_click
    sdRoot = 
File.DirDefaultExternal & "/"
    
FTP.DownloadFile("/Team/prestadores.txt"False, sdRoot, "PRESTADORESFTP.txt")
    
FTP.UploadFile(sdRoot,"Reg" & d.GetDeviceId  & ".txt"True"/Team/RegTmp" & d.GetDeviceId  & ".txt")
End Sub
when in using debug mode, i see this:

Code:
DownloadService [donesuccessfully=false, hc=anywheresoftware.b4a.http.HttpClientWrapper@44d029d0, jobstatus=0 , notification=1(NotificationNot initialized, status_done=2, status_none=0, status_working=1, target=(OutputStreamNot initialized, url=
I test the apk in my dispositive but dont work
Reply With Quote
  #8 (permalink)  
Old 07-30-2011, 05:15 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,807
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The DownloadService is not related. It is a code module that you have added to your project.

Have you added the FTP_DownloadCompleted / FTP_UploadCompleted subs? Without them you will not see any result.
Reply With Quote
  #9 (permalink)  
Old 08-01-2011, 04:16 AM
Newbie
 
Join Date: Jun 2011
Posts: 9
Default

Quote:
Originally Posted by Erel View Post
The DownloadService is not related. It is a code module that you have added to your project.

Have you added the FTP_DownloadCompleted / FTP_UploadCompleted subs? Without them you will not see any result.
I have a Service module called DownloadService, it's the same like the example http://www.basic4ppc.com/forum/basic...#post43109

And I have this subs...

Code:
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

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
When i run the project step by step, never calls to DownloadService module
Reply With Quote
  #10 (permalink)  
Old 08-01-2011, 05:50 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,807
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Try to create a simple project which downloads a single file and see if it works. Make sure to check the logs (including the unfiltered logs if you don't see any message).
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android JSON tutorial Erel Basic4android Getting started & Tutorials 33 05-07-2013 02:08 PM
Android Serial tutorial Erel Basic4android Getting started & Tutorials 119 03-21-2013 11:18 PM
Android Network Tutorial Erel Basic4android Getting started & Tutorials 85 03-08-2013 06:40 PM
Android JSON Tutorial klaus German Tutorials 0 02-11-2011 10:31 PM
Android Serial Tutorial klaus German Tutorials 0 01-26-2011 09:05 PM


All times are GMT. The time now is 05:39 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0