Android Question How to check file size before downloading it

adrianstanescu85

Active Member
Licensed User
Longtime User
Hello all,

I am interested in notifying a user in my app if there's a new version of a map which is available (for instance) at
http://www.openandromaps.org/maps/europe/Malta.zip
so I would need to do this before actually downloading it. If the file size would differ, then the download would make sense.

How can I check the file size without actually downloading the file first?

Thank you!
Adrian
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     hc.Initialize("hc")
   End If
   Dim req As HttpRequest
   req.InitializeHead("http://www.openandromaps.org/maps/europe/Malta.zip")
   hc.Execute(req, 1)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Log("File size: " & Response.ContentLength)
   Response.Release
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log("error: " & StatusCode)
   Response.Release
End Sub

Note that this link doesn't seem to work.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
How can I use this on Main? Cause it is likely to return an error since it's an I/O procedure. Would it help if this were inside another thread (service module)? If so, how?
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
Yes, I did, I found this is the cause for android.os.NetworkOnMainThreadException. Everything else went well on HttpUtils2, but this code above sets this error when launched in the Main activity, only on Wifi (not 3G).
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
I've tried eliminating everything from ResponseSuccess and ResponseError, meaning this is the remaining:

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  Response.Release
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
  Response.Release
End Sub

and still the error is the same. hc is still a HttpClient variable declared under Process_Globals.

It seems this happens just by using the HttpResponse, and I also don't understand why this happens only over non-Wifi access.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
To make things as clear as possible, I just started a brand new B4A project, checked the HTTP lib and nothing else (besides Core), no other modifications, and here is the code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim hc As HttpClient
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    If FirstTime Then
       hc.Initialize("hc")
    End If
    Dim req As HttpRequest
    req.InitializeHead("http://www.openandromaps.org/maps/europe/Romania.zip")
    hc.Execute(req, 1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  'Log("File size: " & Response.ContentLength)
  Response.Release
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
  'Log("error: " & StatusCode)
  Response.Release
End Sub

Layout1 is basically a blank layout, just something to put there instead of nothing.

The error with android.os.NetworkOnMainThreadException remains true whenever using the mobile data plan. I'm using this on Android 4.1.2
 
Last edited:
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
In this case where only the head is initialized, what would be the proper way to use Response.GetAsynchoronously (since it requires an event name and an output stream)?
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
That's useful, true, but I wonder why this error only occurs only when using 3G. When on WiFi everything rolls just as expected, no error pops up, so it doesn't look like any rule violation. Why would this only happen on 3G..?
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
Since this application is already on Google Play and people will need to use it more and more (it's for specialized medical staff), it's a must that 3G access should work. So whenever I test it, I do it independently of my computer, meaning I always set the debug mode to Release (obfuscated).

I attached here an archive with a very simplified project that does just that: check the file size, and in different situations outputs the "network on main thread exception" error.

The cases when the error does NOT appear:
* app on mobile phone debugged in Release (obfuscated) mode, mobile phone NOT connected to B4A in any way, WiFi internet access turned ON
* app on mobile phone debugged in Release mode, mobile phone NOT connected to B4A in any way, WiFi internet access turned ON
* app on mobile phone debugged in Debug (legacy), mobile phone connected to B4A by USB, WiFi internet access turned ON
* app on mobile phone debugged in Debug (rapid), mobile phone connected to B4A by USB, WiFi internet access turned ON
* app on mobile phone debugged in Debug (rapid) mode, mobile phone connected to B4A by USB, WiFi internet access turned OFF, so 3G is ON

The cases where the error DOES appear:
* app on mobile phone debugged in Release (obfuscated) mode, mobile phone NOT connected to B4A in any way, WiFi internet access turned OFF, so 3G is ON
* app on mobile phone debugged in Release mode, mobile phone NOT connected to B4A in any way, WiFi internet access turned OFF, so 3G is ON
* app on mobile phone debugged in Debug (legacy) mode, mobile phone connected to B4A by USB, WiFi internet access turned OFF, so 3G is ON

So using the Debug (rapid) helps, since it disables the error, but obviously the app is computer-dependent at that time. The question still remains why does this occur in Release modes only on 3G and never on Wifi.
 

Attachments

  • TestMain.zip
    498.6 KB · Views: 194
  • Error on Debug Legacy.png
    182.8 KB · Views: 288
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…