I need to do both of these download tasks (with targetSDK=22):
I am able to download the HTML from a website using 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 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:
I spent over a day trying different things to get it to work so that I can do both tasks, 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 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:
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
I spent over a day trying different things to get it to work so that I can do both tasks, so any help would be greatly appreciated.