Android Question Can't download pdf file in webview

Baris Karadeniz

Active Member
Licensed User
I Can't download pdf file in webview with the codes below? Is there any idea? I use HttpUtils2 library.

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
   If Url.EndsWith(".pdf") Then
        Dim j As HttpJob
     j.Initialize("pdf", Me)
     j.Download(Url)
     Return True
    Else If Url.IndexOf("mailto:") > -1 Then 
     Dim Intent1 As Intent
     Intent1.Initialize(Intent1.ACTION_VIEW, Url)
     StartActivity(Intent1)
     Return True
   Else
     Return False
   End If
End Sub

Sub JobDone (Job As HttpJob)

End Sub
 

DonManfred

Expert
Licensed User
Longtime User
An empty jobdone will not save any download to a file.
See the httputil tutorials or search the forum on how to USE IT.
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
I did as you said. I used Erel's "LargeFileDownload.zip" example. I added libraries which Erel said. But I received error logs below;

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
   If Url.EndsWith(".pdf") Then
        Dim dd As DownloadData
        dd.url = Url '<--- download link
        dd.EventName = "dd"
        dd.Target = Me
        CallSubDelayed2(DownloadService, "StartDownload", dd)
     Return True
    Else If Url.IndexOf("mailto:") > -1 Then 
     Dim Intent1 As Intent
     Intent1.Initialize(Intent1.ACTION_VIEW, Url)
     StartActivity(Intent1)
     Return True
   Else
     Return False
   End If
End Sub

Sub dd_Complete(Successful As Boolean)
    Log("Job completed: " & Successful)
End Sub


Error logs;

Installing file.
PackageAdded: package:app.taksim
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (downloadservice) Create **
** Service (downloadservice) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
app.taksim.main:_dd_complete, [[downloadservice=null, errormessage=, httputils2service=null
, jobname=[url]http://www.taksi-m.com.tr/TAKSIMCustomerUserManuelR2_EN.pdf[/url], main=null, password=
, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@427b2a88, starter=null, username=
, tag=[CountingStream=(MyOutputStream) anywheresoftware.b4a.randomaccessfile.CountingStreams$MyOutputStream@428b6c90, Data=[EventName=dd, url=[url]http://www.taksi-m.com.tr/TAKSIMCustomerUserManuelR2_EN.pdf[/url], Target=class app.taksim.main
, IsInitialized=false], Total=58162
, IsInitialized=true], target=class app.taksim.downloadservice, taskid=1
, success=true]]
Error occurred on line: 57 (Main)
java.lang.IllegalArgumentException: argument 1 should have type boolean, got app.taksim.httpjob
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$2.run(BA.java:328)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
This is small project. Zip file is given attached. You can see pdf files in;

1) Taxi Driver Login - Driver Registration - Contract
2) After Customer login at the ? mark at the top of map. (Username: baris--karadeniz@hotmail.com , password: 123456)
 

Attachments

  • Taksim.zip
    16.8 KB · Views: 222
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub dd_progress(count As Long,total As Long )
   
End Sub

Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "l.bal", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Job.Release
End Sub

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (downloadservice) Create **
** Service (downloadservice) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Job completed: true
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Your code
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "l.bal", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
    Job.Release
End Sub


saves the pdf as l.bal in DirDefaultExternal.
 
Last edited:
Upvote 0

Baris Karadeniz

Active Member
Licensed User
Ok. I saw it in the "files" directory. I have 2 questions;

1) pdf files were in 2 places as I wrote above. The first place (Taxi Driver Login - Driver Registration - Contract) is not downloaded. When I press on it nothings happen. (Service doesn't start so job is not completed) What can be the reason. It is in the pop-up menu.

2) After downloading l.bal, I want to open on the screen immediately. Should I write into "Sub dd_complete"? And what I need to write?

Thanks for your help.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Contract) is not downloaded
I cant reach the popup once logged in. There is no button to logout
But i guess you need to capture it in override url, extract the filename and remember it. Or get the url in dd_complete again to extract the url to get the filename out of it...

The url i get in my pc browser points to http://www.taksi-m.com.tr/TAKSIMDriverContractR4_DE.pdf
So i guess you should be able to download the pdf manually with httputils
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
If you turn your phone horizontally, there is a log out button at the top right corner.

Before login did you try to push on "contract"? It is also URL like the pdf after login. It is totally same procedure. Why I need to write something else?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Only way to remove app and install again.
I did!
Now i´m able to click the link for the contract.
The service is started and download th file.
But again your code save it to the same name again l.bal

But it is working

LogCat connected to: 9885e6514556383552
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Service (downloadservice) Create **
** Service (downloadservice) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Job completed: true
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
There happens nothing with my phone (Note II) May be there is a problem with phone. I will try it with another phone. It is not important the name is again l.bal. Because it will be opened as soon as downloading finish. And it will change if another pdf file is downloaded. I think it will be downloaded onto old l.bal.

After downloading l.bal, I want to open on the screen immediately. Should I write into "Sub dd_complete"? And what I need to write?
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
With the codes below?

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
   If Url.EndsWith(".pdf") Then
     Dim j As HttpJob
     j.Initialize("pdf", Me)
     j.Download(Url)
     Return True
   Else
     Return False
   End If
End Sub

Sub JobDone (Job As HttpJob)
End Sub
 
Upvote 0
Top