Android Question Re-use HttpJob sequentially?

william_chen

Member
Licensed User
Longtime User
Hi guys,

I understand that a HttpJob (referring to the HttpJob in OKHttpUtils2) should not be re-used to submit concurrent connection.
But I tried to re-use it in a sequential way?

Many times, App uses a HttpJob to login Web Server. In order to keep track of the user session, web server will return a cookie in http header. Now, AFTER app receive successful response, is it OK to reuse this HttpJob object (without Release, Dim or Initialize again) and submit future request? This way, the future http request will contain the cookie automatically.

I did try the above approach in my project. It seems to work fine most of the time. But occasionally, it seems the HttpJob object does not send data to server.

I check the source code of OKHttpUtils2 and could not find any reason why I cannot re-use HttpJob sequentially. Actually, in HttpUtils2, OkHttpClient is re-used by multiple HttpJob concurrently.

(By the way, I did try the method in Forum and retrieve cookie from first HttpJob object and assign cookie to another HttpJob object but failed. That is another story.)

Now, my questions is:
1) Is it OK to re-used HttpJob sequentially?
2) If not, what is the reason?

Thank you very much.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This way, the future http request will contain the cookie automatically.
There is no relation between cookies and HttpJobs. The cookies will be sent automatically anyway as long as the same OkHttpClient is used (which is the case with OkHttpUtils2).

You can reuse the same HttpJob and send it from the JobDone event. You should call Job.Release. It will delete the previous request stored data.
 
Upvote 0

william_chen

Member
Licensed User
Longtime User
Many thanks to Erel.

I notice that: inside HttpJob class, there is a member: Private req As OkHttpRequest

And the HttpJob download method is implemented as following:
Public Sub Download(Link As String)
req.InitializeGet(Link)
CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

Now, if I re-use HttpJob in the JobDone event, then the req will be Initialze again.
Will such re-Initialization cause any problem?

Thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…