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

aidymp

Well-Known Member
Licensed User
Longtime 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!

** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
MAC = 58:12:43:A7:B7:54
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Error occurred on line: 46 (httputils2service)
java.lang.RuntimeException: Field: CountingStream not found in: java.lang.Object
at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:833)
at anywheresoftware.b4a.shell.Shell.setField(Shell.java:581)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:317)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4430)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **



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

B4X:
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
 

aidymp

Well-Known Member
Licensed User
Longtime 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.

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!

B4X:
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!

B4X:
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

B4X:
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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If j.Tag <> Null And j.Tag Is JobTab Then
dim
jt As JobTab = ...
Shouldnt it be
B4X:
If j.Tag <> Null And j.Tag Is JobTag Then
dim jt As JobTag  = ...
?
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Im not a my machine but just so i understand (still a learner)

B4X:
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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe I need some coffee, however I don't see any difference between the two code snippets...
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!?
 
Upvote 0

aidymp

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

B4X:
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!
 
Upvote 0
Top