Android Question [SOLVED] Not possible to use Wait For with OkHttpResponse.StreamFinish?

Sandman

Well-Known Member
Licensed User
I’m using a slightly modified OkHttpUtils. (It’s modified to be more specialized when communicating with the API on my server.)

In it there's an event _ResponseSuccess that contains this:
B4X:
' Response is an OkHttpResponse
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)

The GetAsynchronously causes an event to the sub below, just as we’re used to:

B4X:
Private Sub Response_StreamFinish (Success As Boolean, theJobId As Int)
    DoThings
End Sub

For a couple of different reasons I wanted to update and modernize this to use Wait For instead, like so:

B4X:
' Response is an OkHttpResponse
Wait For (Response.GetAsynchronously("", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)) StreamFinish (Success As Boolean, theJobId As Int)

DoThings

But that didn’t work at all. It crashes with this error message:

B4X:
java.lang.RuntimeException: java.lang.Exception: Sub _streamfinish was not found.

So even though I’m using Wait For, it doesn’t ”catch” the event. Am I doing something wrong here, or what’s going on?
 

OliverA

Expert
Licensed User
Are you sure you should not use Response_StreamFinish instead of just StreamFinish in your Wait For statement?
Edit: ignore. I see what you are doing.
 

Erel

Administrator
Staff member
Licensed User
Rewriting HttpUtils2Service sounds like a mistake to me.

About your question:
1. Response.GetAsynchronously doesn't return the event "sender" object so you cannot use it as the sender filter parameter.

2. The correct syntax is:
B4X:
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)
Wait For (Response) Response_StreamFinish (Success As Boolean, theJobId As Int)
 

Sandman

Well-Known Member
Licensed User
Rewriting HttpUtils2Service sounds like a mistake to me
Well, it wasn’t my first choice. :) I’d rather have used it as is, but I required the http status code, which I couldn’t get otherwise. (I posted a Wish for it a year ago.) And in any case it’s not a rewrite; just a gentle modification.

Thanks for the corrected syntax, I clearly haven’t understood all aspects of resumable subs yet. I’ll revisit your documentation on it.
 

OliverA

Expert
Licensed User

Sandman

Well-Known Member
Licensed User
Since version 2.70 you have access to the status code via the Response object.
Yeah, I saw something about that, but that boat sailed many moons ago. I'm not going to rip out my battle-tested solution now to replace it with something else, even if it is the official and recommended solution.
 

Sandman

Well-Known Member
Licensed User
The correct syntax is:
B4X:
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)
Wait For (Response) Response_StreamFinish (Success As Boolean, theJobId As Int)
I can’t get that to work. No crash or anything, it just never reaches the Wait For.

This is my code, after your syntax correction:

B4X:
Private Sub http_client_ResponseSuccess (Response As OkHttpResponse, theJobId As Int)

    Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)

    Wait For (Response) Response_StreamFinish (Success As Boolean, theJobId As Int)

    CompleteJob

End Sub

However, if I remove the Wait For and use a sub instead it works perfectly:

B4X:
Private Sub http_client_ResponseSuccess (Response As OkHttpResponse, theJobId As Int)

    Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)

End Sub


Private Sub Response_StreamFinish (Success As Boolean, theJobId As Int)

    CompleteJob

End Sub

From my somewhat naïve understanding of resumable subs your syntax correction looks perfectly reasonable, so I can’t really understand what the problem could be..?
 

Erel

Administrator
Staff member
Licensed User
The problem is related to the "sender" value of Response.GetAsynchronously. It is not set correctly in the library.

You will need to change your code to:
B4X:
Wait For Response_StreamFinish (Success As Boolean, theJobId As Int)
However this means that it will only work properly if there is a single request each time.

Use the sub here instead of wait for.
 

Sandman

Well-Known Member
Licensed User
Use the sub here instead of wait for.
Thanks Erel.

As I’m trying to understand Resumable Subs better, I have to ask: Was it even possible for me to understand this myself, or was this something only you could figure out as you could peek into the inner workings of Response.GetAsynchronously?
 

Erel

Administrator
Staff member
Licensed User
Was it even possible for me to understand this myself
It is possible but not so simple.

First step is to remove the sender filter. It would have then worked.
Next step is to check the value of Sender object. You can see that it doesn't match the value of Response so Response cannot be used as the sender filter parameter.
 
Top