Bug? HTTPUtils2 don't work in a service

gameone

Member
Licensed User
I guess there are a bug or problem with HTTPUtils2 when it's used under a service look here:
https://www.b4x.com/android/forum/threads/http_get-string.61081/

Okay bro i got it, this code work really fine in Main.
But that don't work if i use same code into a service.
(i put all fines, and put code Activity_Create into Service_Start)
Of course i start the service by put in Main Activity "StartService(MyService)"

Anyone can help me ?
Thanks
B4X:
Sub Globals
    Dim hc As HttpClient

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
'    Activity.LoadLayout("Layout1")

Msgbox("hello","hello")
If FirstTime Then
       hc.Initialize("hc")
    End If
    Dim req As HttpRequest
    req.InitializeGet("http://192.168.41.3/android.php")
    hc.Execute(req, 1)
  
  
  StartService(tt)



End Sub


Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim resultString As String
    resultString = Response.GetString("UTF8")
    Msgbox(resultString,"test")
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("Error connecting: " & Reason & " " & StatusCode)
      Msgbox("ERROR","test")
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
  
End Sub


This specific code won't work if instead of put in main activity i put Main Activity code into a service
(Service_Start)
Too if i put into Service_Start i must delete the condition "If FirstTime Then"

So it's a bug? Or a problem of me? If it's about me what the issue to fix that?
Thanks,
 

gameone

Member
Licensed User
@b4a

Then can u give me a working example for what i try to achieve in a service?
Because my code work in main activity but no into a service --"
I would like to know why and got a example or told me a error.

Because i found nothing in your documentation about...

Thanks,
 

gameone

Member
Licensed User
Thanks you bro going check it :)

Bro i just tested in your code, no error but that don't log at all...
Maybe because i use local ip ?

All work fine under main activity but your code won't work bro into service.
I exactly do like you provide me..


HERE IS ERROR:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (tt) Create **
** Service (tt) Start **
** Activity (main) Pause, UserClosed = false **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
[errormessage=, httputils2service=null, jobname=j
, password=, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@b203f700, username=
, tag=java.lang.Object@b203f718, target=class b4a.example.tt, taskid=1
, success=true]



Nevermind, i found the issue.

I changed Sub JobDone with this


B4X:
   If Job.Success = True Then
     
     Log(Job.GetString)
   End If
   Job.Release


So if apps reach and got success with request program log String. (value found on webserver)


Thanks u for your support bro ;)
 
Last edited:
Top