I need to do both of these download tasks (with targetSDK=22):
I am able to download the HTML from a website using the standard okHTTPUtils2 (version 2.82) with this code:
And it works great.
Then, I tried to download a 20MB file using this code:
But for some reason, the job always "completes" with the downloaded file being only 40k in size (resulting in an invalid file)
So, then I found this "custom" version of okHTTPUtils2 for downloading large files (and unselected default okHTTPUtils2 v.2.82 and instead using OKHTTP v1.22 with this new "Custom" okHTTPUtils2 lib):
And it properly downloads the 20MB file great!
But when I tried to download the webpage using the same code I posted above but with using this other "custom" okHTTPUtils2, it gave me this error:
It seems the below line is were the error is:
My guess is that to use the downloading service (to download huge files), you need to pass the custom "Tag" type. But you don't pass a TAG type when using the regular Job.Download method, so it errors when the job.download method tries to use the same routine that the download service uses (which assumes the TAG type is being used), but with the job.download method the Tag is not used, so it crashes. But this is just a guess.
If I do these below steps, then the regular Job.Download works:
I spent over a day trying different things to get it to work so that I can do both tasks using the same lib, so any help would be greatly appreciated.
- Get HTML from a webpage on a website
- Download a large (20mb) apk file from a website
I am able to download the HTML from a website using the standard okHTTPUtils2 (version 2.82) with this code:
B4X:
Sub cmdGetWebpage_Click
Dim job1 As HttpJob
job1.Initialize("", Me)
job1.Download("https://mywebsite.com/index.html")
End Sub
Sub JobDone (Job As HttpJob)
If Job.Success = True Then
WebView1.LoadHtml(Job.GetString)
End If
Job.Release
End Sub
And it works great.
Then, I tried to download a 20MB file using this code:
B4X:
Sub DownloadAndSave (Url As String, Dir As String, FileName As String) As ResumableSub
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Url)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
File.Copy2(j.GetInputStream, out)
out.Close
End If
j.Release
Return j.Success
End Sub
But for some reason, the job always "completes" with the downloaded file being only 40k in size (resulting in an invalid file)
So, then I found this "custom" version of okHTTPUtils2 for downloading large files (and unselected default okHTTPUtils2 v.2.82 and instead using OKHTTP v1.22 with this new "Custom" okHTTPUtils2 lib):
Download huge files with HttpUtils2
Better to use: https://www.b4x.com/android/forum/threads/simple-progress-http-download.127214/#post-796463 The attached project includes a slightly modified version of HttpUtils2 and a new service named DownloadService. The purpose of DownloadService is to make it simple to download files of...
www.b4x.com
And it properly downloads the 20MB file great!
But when I tried to download the webpage using the same code I posted above but with using this other "custom" okHTTPUtils2, it gave me this error:
B4X:
Logger connected to: 192.168.1.109:5555
--------- beginning of system
--------- beginning of main
--------- beginning of crash
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
httputils2service_hc_responsesuccess (java line: 178)
java.lang.ClassCastException: java.lang.Object cannot be cast to b4a.example.download.downloadservice$_jobtag
at b4a.example.download.httputils2service._hc_responsesuccess(httputils2service.java:178)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
java.lang.RuntimeException: java.lang.ClassCastException: java.lang.Object cannot be cast to b4a.example.download.downloadservice$_jobtag
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:233)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to b4a.example.download.downloadservice$_jobtag
at b4a.example.download.httputils2service._hc_responsesuccess(httputils2service.java:178)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
... 8 more
It seems the below line is were the error is:
B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
' ********** Modified code *************
Dim cs As CountingOutputStream
cs.Initialize(File.OpenOutput(TempFolder, TaskId, False))
Dim j As HttpJob = TaskIdToJob.Get(TaskId)
Dim jt As JobTag = j.Tag '<================ Error Line
jt.CountingStream = cs
jt.Total = Response.ContentLength
If jt.Data.url = "" Then
Log("Job cancelled before downloaded started")
cs.Close
End If
Response.GetAsynchronously("response", cs , _
True, TaskId)
'**************************************
End Sub
My guess is that to use the downloading service (to download huge files), you need to pass the custom "Tag" type. But you don't pass a TAG type when using the regular Job.Download method, so it errors when the job.download method tries to use the same routine that the download service uses (which assumes the TAG type is being used), but with the job.download method the Tag is not used, so it crashes. But this is just a guess.
If I do these below steps, then the regular Job.Download works:
- Remove the three modules that are part of the "custom" okHTTPutils2 lib
- Remove "okHTTP" (v 1.22) and replace it with the standard okHTTPUtils2 (v 2.82)
- Comment out the lines that now give an error in the code because we are no longer using the "Custom" lib
- Run the app and click "Get" and it will not error and properly display the webpage.
I spent over a day trying different things to get it to work so that I can do both tasks using the same lib, so any help would be greatly appreciated.
Attachments
Last edited: