Android Question [Solved] How I can get the date/time a web file before download it?

asales

Expert
Licensed User
Longtime User
I want to download a file (html/xml) but before the download, I want to check if the file is newer than the last file that I downloaded.

How can I check it?

Thanks in advance for any tip.
 

DonManfred

Expert
Licensed User
Longtime User
If it is your Webserver and your files then you could use a php script for example which returns the dates of files

http://snapshots.basic4android.de/imagesizes.php

PHP:
<?php
header("content-type: text/plain");
$result = array();

foreach (glob('/absolute/path/to/images/*.png') as $filename) {
    $thisfile = array();

    $path_parts = pathinfo($filename);

    $thisfile["filename"] = $path_parts['basename'];
    $thisfile["ext"] = $path_parts['extension'];
    #echo $filename." - Größe: " . filesize($filename) . "\n";
    $size = getimagesize( $filename);
  $thisfile["width"] = $size[0];
  $thisfile["height"] = $size[1];
  # echo "Bildbreite: " . $size[0];
  # echo "Bildhöhe: " . $size[1];
  $result[] = $thisfile;
}
echo json_encode($result);
?>

[{"filename":"0024.png","ext":"png","width":407,"height":746},{"filename":"0025.png","ext":"png","width":338,"height":574},{"filename":"zip0029.png","ext":"png","width":238,"height":210},{"filename":"zip0030.png","ext":"png","width":512,"height":254}]
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I was thinking if is possible to use a library like httputils and informe the url and get the timestamp of the file.
You can use httputils for this.

BUT the server must implement a method which returns the filedate.

My php above is just an example you can easily adapt to not check the imagessizes but to check the filedate and then return it.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I tried to use the code below, but I get the date = 01/17/1970 in all dates.

Where I am going wrong?
How to fix to display the date of the file?

Thanks for any tips to correct and improve the code.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
    'Send a GET request
    job1.Download("http://snapshots.basic4android.de/imagedates.php")
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
           Dim res As String
        res = Job.GetString
               
        Dim parser As JSONParser
        parser.Initialize(res)

        Select Job.JobName
            Case "Job1"
                   Dim ListOfAttributes As List
                Dim FileName As String
                Dim FileDate As Long
               
                ListOfAttributes = parser.NextArray 'returns a list with maps
                               
                For i = 0 To 5
                    Dim MyFiles As Map
                    MyFiles = ListOfAttributes.Get(i)
                                           
                    FileName = MyFiles.Get("filename")
                    FileDate = MyFiles.Get("mtime")
                       
                    Log(FileName & " - " & " - " & DateTime.Date(FileDate))  'date = 01/17/1970       
                Next   
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 
Upvote 0
Top