Android Question Http library replacing

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi to all. As the title of this post, I need to update a very old project to work in Android 10. The most critically parts are the http requests: I read new posts about this, but unfortunately I do not understand how to modify them. An example code is here:

B4X:
Sub ClientPosti_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   
    Dim ResponseData As String
    ResponseData = Response.GetString("UTF8")
   
    Response.Release
   
'    Response example
'    *OK*@{"status": 0,
'            "posto": ["9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
'        }

'   Work with ResponseData string...

I receive a string response like this. I tried to sitch to OkHttpResponse but the GetString method in Response no longer exists.
How can I do the update? Thanks to all!
 

DonManfred

Expert
Licensed User
Longtime User
Upload a small project with the working code

Using okhttputils2 IS the solution! You just need to do it right.

 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi @Erel , I have a global function that handles, basing on TaskId, all old http requests. In this case, how can I replace with new library? I explain...


B4X:
Sub ExecuteRemoteQuery (RemoteQuery As String, TaskId As Int)
 
    Dim strRemoteQuery As String
    strRemoteQuery = "cmd=" & RemoteQuery & "&idmacchina=" & IdMacchina & "&versione=" & VersionNumber
 
    Dim HRequest As HttpRequest
    HRequest.InitializePost2(ServerAddress & "/" & ScriptName,strRemoteQuery.GetBytes("UTF8"))
 
    Select TaskId
     
        Case 1
            Main.Client.Execute(HRequest,TaskId)
         
        Case 2
            SendData_Service.ClientBiglietti.Execute(HRequest,TaskId)

     'etc...

Can I Initialize the HttpJob so the JobDone event in every activity module will be invoked? Something like this:

B4X:
Dim j As HttpJob
j.Initialize("Client_JobDone", "Main")
'etc...

And... In this mode how can I work with the "Wait For (j) JobDone(j As HttpJob)"?
 
Last edited:
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Start with the above tutorial. You are not creating HttpJob correctly.
Yes I know, but the question is that I have the "ExecuteRemoteQuery" sub (like in my previous answer) in a static module and I can't put into the Wait For of the HttpJob... So I think, can I use the Initialize method of the HttpJob, setting the jobdone sub and the relative module that handles it?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't waste your time with the static code module. Move the code to a class. As you are not using B4XPages, best to initialize that class in Starter.Service_Create.

It will be simple to delegate the response to anywhere you like.

Though, once you see how it is simple to make requests with Wait For, you will not need any special module for this.
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Don't waste your time with the static code module. Move the code to a class. As you are not using B4XPages, best to initialize that class in Starter.Service_Create.

It will be simple to delegate the response to anywhere you like.

Though, once you see how it is simple to make requests with Wait For, you will not need any special module for this.
Thanks for the reply, but it is complex for me to understand how to do this, because this is a very old and big project. Can you explain me that with an example? Thank you very much
 
Last edited:
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
You can still use the same design as before:
B4X:
Sub ExecuteRemoteQuery (RemoteQuery As String, TaskId As Int)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(RemoteQuery)
Wait For (j) JobDone (j As HttpJob)
If j.Success Then
   Select TaskId
      Case 1
          CallwhateverYouLIke
  End Select
End If
j.Release
Thank you. Thus you don't recommend me to use the Initialize method of the HttpJob, to set the JobDone event to handle requests (instead of Select case statement)?
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
The recommended way is shown above. It is simpler as you can handle everything in the same place.
Thanks.
Another question. I had a HTTP post bytes method, I have to replace it with:
B4X:
Dim HJRequest As HttpJob
HJRequest.Initialize("",Me)

HJRequest.PostBytes(...)
'or
HJRequest.GetRequest.InitializePost2(...)
'and what is the difference?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Top