Android Question HTTP Download

prokli

Active Member
Licensed User
Longtime User
I uses this code several time, as shown here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("LayoutMain")
    httpJob1.Initialize("httpJob1", Me)
End Sub

Sub VRec_Result (Success As Boolean, Texts As List)
    '----------------------------------------------------------
    ' VRec_Result Event fired by voice recogition module
    '----------------------------------------------------------
    Dim url As String
    If Success = True Then
        url = "https://www.glosbe.com/de......."
        httpJob1.Download(url)   
    End If
nd Sub

Private Sub JobDone(Job As HttpJob)

    Dim TextReader1 As TextReader
    If Job.Success = True Then
        TextReader1.Initialize(Job.GetInputStream)
        Log(TextReader1)
    End If
End Sub

Whenever the code line "httpJob1.Download(url)" is executed I get this error message:

java.lang.NoSuchMethodError: No static method onStartCommand(Lanywheresoftware/b4a/BAZ in class Lanywheresoftware/b4a/objects/ServiceHelper$StarterHelper; or its super classes (declaration of 'anywheresoftware.b4a.objects.ServiceHelper$StarterHelper' appears in /data/app/b4a.parrot-2/base.apk)

So, I am getting crazy since I used this code several times in other app never keeping any issues. What is going wrong here???
 

prokli

Active Member
Licensed User
Longtime User
Found solution by myself ?

I forgot to take "OkHttpUtils2" instead of "HttpUtils2".
I am getting old and forgetful ?
 
Upvote 0

prokli

Active Member
Licensed User
Longtime User
I changed my code according to the one as provided by Erel and it works also fine for me.
But why is this code WRONG??

event procedure:
Private Sub JobDone(Job As HttpJob)

    Dim TextReader1 As TextReader
    If Job.Success = True Then
        TextReader1.Initialize(Job.GetInputStream)
        Log(TextReader1)
    End If
End Sub
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
It's out of dated. Use something like following instead
B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release
 
Upvote 1

prokli

Active Member
Licensed User
Longtime User
Okay?! Out of date does not mean wrong!
B4X:
        Wait For (jb) JobDone(jb As HttpJob)
The code above generates a warning telling me that "jb is not initialized. Of course, it is not an error but still a warning. When I explicitly define "jb" the code does not work at all!
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Dim TextReader1 As TextReader If Job.Success = True Then TextReader1.Initialize(Job.GetInputStream) Log(TextReader1)
Using JobDone sub is not only a deprecated way, another mistake is if the API returns String result then correct way is to use:
B4X:
Log(Job.GetString)
.
You also need to release the Job object. The older way also introduce other problems if you have more than one httpjobs. You need to check the jobs name and it makes more complicated to catch the error if the job Success is false.
So these are the areas your code should have been corrected and writing the code as posted by toby is a correct way. You may argue it is not wrong but it is a “Code Smell”.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…