Android Question OkHttpUtils2 v3.02 Not Working In Receiver

Plutoam

Member
Hello, I am using the latest update of the OkhttpUtils2 library, which is version 3.02, in the FirebaseMessaging Receiver, some requests are not executed.
It gives very strange errors
Sometimes it even gives an error, but the request is executed and sent to the server
And when the program is closed, my request is sent to Firebase and even Http2Service is started, but the request is not sent.
What is wrong ?

Errors:
*** Receiver (httputils2service) Receive ***
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "MyDomain.Com": No address associated with hostname, Response:

And let me add that the domain to which I send the request has no problem
Requests are sent to the domain even as long as the program is not closed
But as soon as I close the program, either the request does not go, or I receive this error
Sometimes a request is sent even with an error

Here Is My Code in FirebaseMessaging Receiver

My Codes:
jsondata.Initialize
jsondata.Clear
jsondata.Put("action","newdevice")
jsondata.Put("androidid",androidid)
jsondata.Put("model",model)
sendreq("https://myDomain.com",jsonen(jsondata))

Sub jsonen(data As Map) As String
    json.Initialize(data)
    Return json.ToString
End Sub

Sub sendreq(url As String,data As String)
    ht.Initialize("",Me)
    ht.PostString(url,data)
    ht.Release
End Sub


* and I tested SDK 30, SDK 31, SDK 32, SDK 33 and I still have the same problem.
 
Solution
It is possible that when the device wakes up because of the push notification the network stack isn't fully ready. Overall making http calls from a receiver can be problematic as the process can be killed after a few seconds, however in this case it looks like something isn't ready.
Make some tests. Add Sleep(3000) before making the request.

Plutoam

Member
Your code is wrong. First step is to send the request correctly: [B4X] OkHttpUtils2 with Wait For

Sending requests from a receiver can be problematic however you should first fix your code.
As you said, I changed my code, but it still gives an error

Logs:
*** Receiver (httputils2service) Receive (first time) ***
*** Receiver (httputils2service) Receive  ***
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "MyDomain.com": No address associated with hostname, Response:

Request Sub:
Sub jsonen(data As Map) As String
    json.Initialize(data)
    Return json.ToString
End Sub

Sub sendreq(url As String,data As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(url,data)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("OK")
    End If
    j.Release
End Sub


Codes:
jsondata.Initialize
jsondata.Clear
jsondata.Put("action","newdevice")
jsondata.Put("androidid",androidid)
jsondata.Put("model",model)
sendreq("https://myDomain.com",jsonen(jsondata))
 
Upvote 0

Plutoam

Member
As you said, I changed my code, but it still gives an error

Logs:
*** Receiver (httputils2service) Receive (first time) ***
*** Receiver (httputils2service) Receive  ***
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "MyDomain.com": No address associated with hostname, Response:

Request Sub:
Sub jsonen(data As Map) As String
    json.Initialize(data)
    Return json.ToString
End Sub

Sub sendreq(url As String,data As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(url,data)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("OK")
    End If
    j.Release
End Sub


Codes:
jsondata.Initialize
jsondata.Clear
jsondata.Put("action","newdevice")
jsondata.Put("androidid",androidid)
jsondata.Put("model",model)
sendreq("https://myDomain.com",jsonen(jsondata))
I should also add this, it has this problem when the program is closed
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is possible that when the device wakes up because of the push notification the network stack isn't fully ready. Overall making http calls from a receiver can be problematic as the process can be killed after a few seconds, however in this case it looks like something isn't ready.
Make some tests. Add Sleep(3000) before making the request.
 
Upvote 0
Solution

Plutoam

Member
It is possible that when the device wakes up because of the push notification the network stack isn't fully ready. Overall making http calls from a receiver can be problematic as the process can be killed after a few seconds, however in this case it looks like something isn't ready.
Make some tests. Add Sleep(3000) before making the request.
And should I consider this the best solution or are there other solutions?
 
Upvote 0
Top