Android Question Parallel HttpJob requests

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi, I have this class:

[CODE lang="b4x" title="HttpTask"]Sub Class_Globals

Dim ModuleCallback As Object
Dim SocketEvent As String

Dim HJRequest As HttpJob

End Sub

Public Sub Initialize (mCallback As Object, sEvent As String)

ModuleCallback = mCallback
SocketEvent = sEvent

HJRequest.Initialize("",Me)

End Sub

Public Sub ExecuteRemoteQuery (RemoteQuery As String, TaskId As Int)

Dim RemoteQueryStr As String = "cmd=" & RemoteQuery & "&version=" & Starter.VersionNumber

HJRequest.PostBytes(Starter.ServerAddress & "/" & Starter.RemoteQueryScript,RemoteQueryStr.GetBytes("UTF8"))
HJRequest.GetRequest.Timeout = Starter.TimeoutOffline

Wait For (HJRequest) JobDone (HJResponse As HttpJob)

Dim Success As Boolean = HJResponse.Success
If (Success = True) Then

Dim ResponseData As String = HJResponse.GetString2("UTF8")

CallSubDelayed3(ModuleCallback,SocketEvent & "_ResponseSuccess",ResponseData,TaskId)

Else

Dim Response As OkHttpResponse = HJResponse.Response

CallSubDelayed3(ModuleCallback,SocketEvent & "_ResponseError",Response,TaskId)

Response.Release

End If

HJResponse.Release
HJRequest.Release

End Sub[/CODE]

Everything works perfectly. But if there are many parallel request, like:

[CODE lang="b4x" title="Activity sub"]Sub GetDataFromServer

If (Condition1) Then

Dim HT As HttpTask
HT.Initialize(Me,"CRSocket")
HT.ExecuteRemoteQuery("...params...",48)

End If

If (Condition2) Then

Dim HT As HttpTask
HT.Initialize(Me,"MBSocket")
HT.ExecuteRemoteQuery("...params...",50)

End If

If (Condition3) Then

Dim HT As HttpTask
HT.Initialize(Me,"TPSocket")
HT.ExecuteRemoteQuery("...params...",54)

End If

End Sub[/CODE]

I obtain this error:
B4X:
ResponseError - Reason: java.net.SocketTimeoutException

How can I solve this, avoiding making requests one after the other? Thanks
 
Last edited:
How can I solve this, avoiding making requests one after the other?
 
Upvote 0
Thank for reply.
I know, but my question is:
why can't I send multiple httpjob requests at the same time (with different objects, obviously), and I have to wait the completion of a request to execute the next? (as was the case with old HttpRequest)
 
Last edited:
Upvote 0
Once you understand how to work with Wait For, you will see that this class only makes things more complicated. Correcting the bugs in this code will not help in any way.
I made this class as a requests handler to aggregate them in all activities, in order to avoid writing almost the same code in all places.
Since the code in this class is almost identical to what I should write in all places (apart from the initialization), I would like to understand the reason why it is more complicated, and where bugs are inside it. Thank you for all :)
 
Last edited:
Upvote 0
I think the problem is in httpjob referencing, you should try using object array, so all your requests will work in parallel.
Try like this "Not tested"
B4X:
Sub Globals
   
    Dim HJRequest() As HttpJob
   
End Sub

'Each request has unique httpjob object, note TaskId also should be unique
Public Sub ParallelRequest (RemoteQuery As String, TaskId As Int)
    Dim RemoteQueryStr As String = "cmd=" & RemoteQuery & "&version=" & Starter.VersionNumber
    HJRequest(TaskId).Initialize("",Me)
    HJRequest(TaskId).PostBytes("url",RemoteQueryStr.GetBytes("UTF8"))
    Wait For (HJRequest(TaskId)) JobDone (HJResponse As HttpJob)
    Dim Success As Boolean = HJResponse.Success
    If Success Then
        Dim ResponseData As String = HJResponse.GetString2("UTF8")
        'do what you want ...
    End If
    HJResponse.Release
    HJRequest(TaskId).Release  
End Sub
 
Upvote 1
I think the problem is in httpjob referencing, you should try using object array, so all your requests will work in parallel.
Try like this "Not tested"
B4X:
Sub Globals
  
    Dim HJRequest() As HttpJob
  
End Sub

'Each request has unique httpjob object, note TaskId also should be unique
Public Sub ParallelRequest (RemoteQuery As String, TaskId As Int)
    Dim RemoteQueryStr As String = "cmd=" & RemoteQuery & "&version=" & Starter.VersionNumber
    HJRequest(TaskId).Initialize("",Me)
    HJRequest(TaskId).PostBytes("url",RemoteQueryStr.GetBytes("UTF8"))
    Wait For (HJRequest(TaskId)) JobDone (HJResponse As HttpJob)
    Dim Success As Boolean = HJResponse.Success
    If Success Then
        Dim ResponseData As String = HJResponse.GetString2("UTF8")
        'do what you want ...
    End If
    HJResponse.Release
    HJRequest(TaskId).Release 
End Sub
Thank you! I will try...
 
Upvote 0
Back
Top