Android Question Getting fiel size before downlaoding

Devv

Active Member
Licensed User
Longtime User
i found this

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     hc.Initialize("hc")
   End If
   Dim req As HttpRequest
   req.InitializeHead("http://download.thinkbroadband.com/5MB.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

error log

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 36 (Main)
java.lang.NullPointerException
at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.getContentLength(HttpClientWrapper.java:543)
at b4a.example.main._hc_responsesuccess(main.java:406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:334)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Pause, UserClosed = true **

line 36 is Log("File size: " & Response.ContentLength)

it is not working for me , any other way ?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Alternatives:

Are the files you want to download hosted on your own server (hosting for ex)?
Then maybe you can use a small php-script which you can first call
The php reads a directory and put all filenames and sizes into an array.
The array will be returned by the php as JSON

In b4a you do a httputils call to get the directory-listeing from your php file and you get the json from php. Parsing the json will result in all files and filesizes...

But for this you need to write a small "helper-php-script".

Or you could write a small b4j app which does the same as the php-script...
 
Upvote 0
Top