Android Question Multiple JobDone-File Download

Ydm

Active Member
Licensed User
Longtime User
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
 

Ydm

Active Member
Licensed User
Longtime User
I fixed the errors you mentioned. I used xui. But the problem is the same.
 
Upvote 0

Ydm

Active Member
Licensed User
Longtime User
I used File.DirRootExternal to make the downloaded file appear in the Download folder.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public wv1 As WebView
    Private wvs1 As WebViewSettings
    Private wvext1 As WebViewExtras
    Public pushUrl As String
    Public urlAdres As String
    Public deviceIdKaydedildi As Boolean
    Public hc As HttpJob
    Private indirilecekUrl As String
    Dim i As Intent
    Dim CookieManager1 As CookieManager
    Public provider As FileProvider
    Dim rp As RuntimePermissions
    Dim KeyboardSize As Int
    Dim btnRefresh As Button
    Dim tiklananUrl As String
    Private chooser As ContentChooser
    Private filePathCallback As Object

End Sub
 
Upvote 0
Top