When you like to download files with httputils2 you need to know the files by name. There are some examples using ftp or a generated xml file with the contents first. To me this could be easier and more dynamic.
My tutorial uses this php script:
As it uses "opendir('.') it will read all files of the directory/path it is started from. F.e. if you put it in www.yourdomain.com/pictures it will give back all the files in this directory as a JSON encoded array.
In b4a we use it this way:
Coming back from the script:
This code is very simple and effective without any passwords or other user data.
You can call the php script from a browser, too. Just type www.yourdomain.com/pictures/files.php
My tutorial uses this php script:
B4X:
<?php
$return_array = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if($file == "." or $file == "..")
{
}
else
{
$return_array[] = $file;
}
}
echo json_encode($return_array);
closedir($handle);
}
?>
As it uses "opendir('.') it will read all files of the directory/path it is started from. F.e. if you put it in www.yourdomain.com/pictures it will give back all the files in this directory as a JSON encoded array.
In b4a we use it this way:
B4X:
Dim fi As HttpJob
fi.Initialize("getfiles", Me)
fi.Download2("http://www.yourdomain.com/pictures/files.php", _
Array As String("Null", "Null"))
Coming back from the script:
B4X:
Sub JobDone(Job As HttpJob)
If Job.Success Then
Dim res As String
res = Job.GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select Job.JobName
Case "getfiles"
Dim ListOfFiles As List
ListOfFiles = parser.NextArray
If ListOfFiles.Size > 0 Then
For i = 0 To ListOfFiles.Size - 1
Log(">> File: " & ListOfFiles.Get(i))
'here you could start another job to download the files/images/etc.
Next
End If
End Select
Else
ToastMessageShow("Error: " & Job.ErrorMessage, False)
End If
Job.Release
This code is very simple and effective without any passwords or other user data.
You can call the php script from a browser, too. Just type www.yourdomain.com/pictures/files.php