I am using webview in my project. I struggled a lot to download the file properly when a link is clicked in webview. I solved it. Now I have a problem. When the application is first opened, I do version control. A different HttpJob works for this. Then I do the file download job in the wv1_OverrideUrl event. If I comment out the VersionControl function at the opening, the file download works correctly. But if both work together, it downloads a 13 byte file. Is there something conflicting, where am I making a mistake?I used all incoming calls in the JobDone event. The situation was the same there. Then I used it with the wait function like this.
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim wwJO As JavaObject = wv1
wwJO.RunMethod("setWebContentsDebuggingEnabled",Array(True))
wv1.LoadUrl("https://xxx.com/login.php")
provider.Initialize
VersiyonCheck
End Sub
Sub VersiyonKontrol
Dim jv As HttpJob
jv.Initialize("VersiyonSorgula", Me)
jv.Download("https://xxx.com/versiyon.php") ' PHP dosyanı çağır
Wait For (jv) JobDone(jv As HttpJob)
If jv.Success Then
Dim gelenVersiyonStr As String = jv.GetString
gelenVersiyonStr = gelenVersiyonStr.Replace(".", "") ' Noktaları kaldır
Dim sunucuVersiyon As Int = gelenVersiyonStr
Dim mevcutVersiyon As Int = GetUygulamaVersiyonu.Replace(".", "")
Log("Sunucudan gelen versiyon: " & sunucuVersiyon)
Log("Mevcut uygulama versiyonu: " & mevcutVersiyon)
If sunucuVersiyon > mevcutVersiyon Then
If Msgbox2("Yeni bir güncelleme mevcut. İndirmek için lütfen tıklayın!!!", "Güncelleme", "Evet", "", "Hayır", Null) = DialogResponse.POSITIVE Then
ApkIndir
Else
ExitApplication
End If
End If
jv.Release
End If
End Sub
Sub wv1_OverrideUrl (Url As String) As Boolean
Log("Tıklanan link: " & Url)
If Url.Contains(".pdf") Or Url.Contains(".zip") Or Url.Contains("file.php") Then
tiklananUrl = Url 'Url yi JobDone içinde kullanabilmek için
IndirSessionliDosya(Url)
Return True ' WebView açmasın
End If
Return False ' Diğer linkler normal açılsın
End Sub
Sub IndirSessionliDosya(Url As String)
Dim cookie As String = CookieManager1.GetCookie(Url) ' veya domainin
Log("Session Cookie: " & cookie)
Dim jd As HttpJob 'redim and initialize
jd.Initialize("dosyaindir", Me)
' Dosyayı indir
jd.Download(Url)
jd.GetRequest.SetHeader("Cookie",cookie)
ProgressDialogShow("Dosya indiriliyor...")
Wait For (jd) JobDone(jd As HttpJob)
If jd.Success Then
' URL'den "file=" parametresini çekelim
Dim Url As String = tiklananUrl ' veya Job.Tag (hangisinde URL varsa)
Dim fileName As String
Dim m As Matcher
m = Regex.Matcher("file=([^&]+)", Url)
If m.Find Then
fileName = m.Group(1)
Else
fileName = "xx_tmp" ' fallback
End If
'-------------------------------------------------------------
' Aynı isimde dosya varsa önce sil --------------------------
If File.Exists(File.Combine(File.DirRootExternal, "Download"), fileName) Then
File.Delete(File.Combine(File.DirRootExternal, "Download"), fileName)
End If
'------------------------------------------------------------
Dim out As OutputStream = File.OpenOutput(File.Combine(File.DirRootExternal,"Download") , fileName, False)
File.Copy2(jd.GetInputStream, out)
out.Close
' İndirilen dosyanın galeride görünmesi için ------------------------------------------------------------------------
DosyaYenile(File.Combine(File.DirRootExternal, "Download/"&fileName))
'--------------------------------------------------------------------------------------------------------------------
'İndirdiğin dosyanın kontrolü------------------
If File.Exists(File.DirRootExternal & "/Download", fileName) = False Then
Log("Dosya yok!")
Else
Log("Dosya bulundu.")
End If
'---------------------------------------------
DosyaAc(File.Combine(File.DirRootExternal,"Download"),fileName)
ToastMessageShow("İndirme tamamlandı.", False)
Else
Log("İndirme hatası: " & jd.ErrorMessage)
ToastMessageShow("İndirme başarısız!", True)
End If
jd.Release
End Sub
Sub DosyaYenile(u As String)
' İndirilen dosyanın galeride görünmesi için ------------------------------------------------------------------------
Dim i As Intent
i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & u)
i.Flags = 0 ' İstersen ekle
Dim ph As Phone
ph.SendBroadcastIntent(i)
'--------------------------------------------------------------------------------------------------------------------
End Sub