Android Question Help - Download Files & Use PHP with httputils2.bas?

Discussion in 'Android Questions' started by aidymp, May 19, 2015.

  1. aidymp

    aidymp Active Member Licensed User

    Hi, Im using the 3 Modules "DownloadService.bas" "HttpJob.bas" "HttpUtils2Service.bas" I make heavy use of them to Download large files! it all works great!

    Now Im protecting my app, and making a version that only works on devices, that have the mac number in my database! im using PHP and simply posting the mac address to my server, if its in the database it responds yes, and if not it responds no! (This program is based on examples using the httputils2 lib)

    At the moment both programs work well! separately! but im trying to make them into one program.

    The Downloader part downloads large files and uses the progress bar (NEEDED) and works exactly how I want so i dont want to change that! (if i use the lib) i lose the progress bar and gain a system icon!)

    If I use the modules, my PHP requests give the following error!



    looking at the errors! i have changed the code in Sub hc_ResponseSuccess like so!

    Code:
    Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
        
    ' ************* TEST CODE
        Dim job As HttpJob = TaskIdToJob.Get(TaskId)
       job.Tag = Response.StatusCode
       Response.GetAsynchronously(
    "response"File.OpenOutput(TempFolder, TaskId, False), _
         
    True, TaskId)
     
        
    ' ********** 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
        '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
    The problem with that is PHP works fine! but my downloads dont (i just get an icon in the system bar staring my file is downloading! - (I dont want that I want the progress bar!)

    My Question is HOW can I have both, the download and progress, and PHP communication?

    Thanks

    Aidy
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    There is no such thing as "PHP request". It doesn't matter to the client how the web server is implemented.

    Remove the reference to HttpUtils2 and only use the modified source.
     
  3. aidymp

    aidymp Active Member Licensed User

    I have removed the library, just using the Modified source! BUT if i want to use both of these code segments

    This one (what i call the PHP REQUEST) checks if the mac address is on my database - THIS WORKS with the code above, but my download routine does not!

    Code:
    Dim Login As HttpJob
        Login.Initialize(
    "isvalid", Me)   
        Login.Download2(
    "http://blah-blah.co.uk/php/checkmac.php", _
        
    Array As String("Action","isvalid","mac"mac"email"Email))
        Login.Release
        
    ProgressDialogShow("Connecting to server...")
    This one to download my setup files - THIS WORKS (as in allows me to display a progress bar, and download information ) if I change the code like below!

    Code:
    Dim stp1 As DownloadData
        link1=
    "http://mirrors.kodi.tv/releases/android/arm/kodi-14.2-Helix-armeabi-v7a.apk"
        stp1.url = link1
        stp1.EventName = 
    "stp1"
        stp1.Target = Me
        CallSubDelayed2(DownloadService, 
    "StartDownload", stp1)
    Changes made to allow the progress and download information to be displayed

    Code:
    Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
        
    ' ************* TEST CODE
      '  Dim job As HttpJob = TaskIdToJob.Get(TaskId)
      ' job.Tag = Response.StatusCode
      ' Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
       '  True, TaskId)
       ' ********** 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
        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
    So I guess I need to work out how to get both bits of code working together!?!?

    Thanks

    Aidy
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can add a check in the modified code like:
    Code:
    If j.Tag <> Null And j.Tag Is JobTab Then
    dim jt As JobTab  = ...
    Make sure that Response.GetAsynchronous is always called.
     
  5. DonManfred

    DonManfred Expert Licensed User

    Shouldnt it be
    Code:
    If j.Tag <> Null And j.Tag Is JobTag Then
    dim jt As JobTag  = ...
    ?
     
    Last edited: May 20, 2015
    aidymp likes this.
  6. aidymp

    aidymp Active Member Licensed User

    Im not a my machine but just so i understand (still a learner)

    Code:
    Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)

    If j.Tag <> NullAnd j.Tag Is JobTab Then ' ************** HERE ?

       
    ' ********** 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
        jt.CountingStream = cs
        jt.Total = Response.ContentLength
        
    If jt.Data.url = "" Then
           
    Log("Job cancelled before downloaded started")
           cs.Close
        
    End If

     
    Else ' ********************* HERE ?

     
    ' ************* TEST CODE
      Dim job As HttpJob = TaskIdToJob.Get(TaskId)
      job.Tag = Response.StatusCode
      Response.GetAsynchronously(
    "response"File.OpenOutput(TempFolder, TaskId, False), _
      
    True, TaskId)

    End If ' ********************** HERE ?

        Response.GetAsynchronously(
    "response", cs , _
            
    True, TaskId)
        
    '**************************************
    End Sub
    Thanks

    And sorry for being a pain!

    Aidy
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Maybe I need some coffee, however I don't see any difference between the two code snippets...

    @aidymp the code you posted will call Response.GetAsynchronously twice.
     
    aidymp likes this.
  8. DonManfred

    DonManfred Expert Licensed User

    You posted "Is JobTab" and i think this is a spelling mistake and should have be "Is JobTag"
    I didn´t checked it with the httputils-source as i need to find the folder first where i stored them ;) ... Was my first thought after reading this

    Maybe i´m totally wrong!?
     
    aidymp and Erel like this.
  9. Erel

    Erel Administrator Staff Member Licensed User

    You are of course correct.
     
    aidymp and DonManfred like this.
  10. aidymp

    aidymp Active Member Licensed User

    Thank you both of you! I too saw NO difference between DonManfreds and Erels code! - Doh!

    and was working with Erels! and getting an error! lol

    but I can confirm that this code

    Code:
    Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
        
    ' ********** Modified code *************
        Dim cs As CountingOutputStream
        cs.Initialize(
    File.OpenOutput(TempFolder, TaskId, False))
        
    Dim j As HttpJob = TaskIdToJob.Get(TaskId)
        
        
    If j.Tag <> Null AND j.Tag Is JobTag Then 

        
    Dim jt As JobTag = j.Tag

        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)
        
    '**************************************
        Else
        
    ' ************* TEST CODE
         Dim job As HttpJob = TaskIdToJob.Get(TaskId)
       job.Tag = Response.StatusCode
       Response.GetAsynchronously(
    "response"File.OpenOutput(TempFolder, TaskId, False), _
         
    True, TaskId)
        
    End If
      
    End Sub
    Allows BOTH the Downloading of large files (and gives me the progress bar, and info I require) as well as allows me to see the response to a php request (or what ever you call it) I was going to give up and use the AsyncDownloader - but i have the same program in B4J and B4A! and AsyncDownloader will not work in B4J!

    So I really Appreciate it guys!

    Thank you DonManfred & Erel!
     
    AlpVir and DonManfred like this.
  11. DonManfred

    DonManfred Expert Licensed User

    You can show your appreciation giving something back to the helping guys. LIKE their posts.
     
    aidymp likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice