HTTP Utility

CharlesIPTI

Active Member
Licensed User
Longtime User
Can this code set some other module as the CallBack Utility

B4X:
HttpUtils.CallbackActivity = "Main"


I'll need to make calls to this web service frequently in the operation of this app, for example once in the login. and many times later on, to get and update the data it manipulates.

I want to implement a "Special" module that does the web service access and xmlSax parsing ..

how do I structure this ?

The Example shows me the setup of callback module and the jobDoneSub & URlDoneSub in the Activity_Create()

Can I call the HttpUtils.PostString() from my "special" module in a regular sub ?

It doesnt have to be in the Activity create does it ??

Please excuse minor syntax stuff I only worried about the REAL Overall Structure I amy have missed syntax stuff its late in the day


Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
HttpUtilsService uses CallSub to raise the event. You cannot call CallSub with code modules. You have two options:
1. Delegate the events from the activity to the code module. The activity that handles the event must be active for this to work.
2. Modify HttpUtilsService and directly call your code module instead of the CallSub method.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
You can re-designate
B4X:
HttpUtils.CallbackActivity = "Main"
to a different activity instead of "Main". I have an app where the initial login happens in "Main" then once that is successful I start a new new activity to show the options after login and redefine "HttpUtils.CallbackActivity". You must be certain that all the HTTP calls in the first one are complete before you re-designate. All of my calls are daisy chained so a successful return triggers the next one.
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Important Please Asap

Thank you for your reply....

I am reviewing the Sliding Panels Sample now, as it is described as ...
"You can delegate any event. See this example: Android SlidingPanels" and since I am interested in your proposed solution :

#1 Delegate the events from the activity to the code module. The activity that handles the event must be active for this to work.

That last sentence WILL become much more important when I get to the various settings screens and supporting screens which will all assuredly have to make their own web service calls to push and pull data!
REDUNDANCY !!!! :BangHead::BangHead:

However ::
Can anyone please give me little nudge in the right direction,,, I don't quite "SEE" either your solutions as described yet.

:sign0085:

CallSub from the documentation:
"allows an activity to call a service sub or a service to call an activity sub. It cannot be used to call methods in a Code module"

Are you saying the Service module can use CallSub2 ( its currently using version 2 of the CallSub method) to invoke the CallbackActivity & CallbackUrlDoneSub

Which can in turn be defined in my main activity ::: as existing in another module not the main module ?? I'm really confused now :confused:



HttpUtilsService::
B4X:
CallSub2(HttpUtils.CallbackActivity, HttpUtils.CallbackUrlDoneSub, taskToRequest.Get(TaskId))


Main:
Activity_Create:
B4X:
HttpUtils.CallbackActivity = "rfCartDataWebService"  ' Something Other Than MAIN 
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.CallbackUrlDoneSub = "UrlDone"



Or perhaps--> in the HttpUtilsService your saying I can call something directly in another code module INSTEAD of using CallSub to get back to the defined methods for HttpUtils as defined in the Activity_Create of the Main .....
INSTEAD I just "Brute Force" in the HttpUtilsService call, another code modules subs ??

:confused: :sign0085:

OK I'm describing this but I'd assuredly have to follow my own little description to actually try to code this out because I'm still very confused !!


Thank You so much for your help in grasping these concepts ..
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Still Confused

You can re-designate
B4X:
HttpUtils.CallbackActivity = "Main"
to a different activity instead of "Main". I have an app where the initial login happens in "Main" then once that is successful I start a new new activity to show the options after login and redefine "HttpUtils.CallbackActivity". You must be certain that all the HTTP calls in the first one are complete before you re-designate. All of my calls are daisy chained so a successful return triggers the next one.



Ah a Different Activity NOT a different CODE Module....
Interesting.. My current Activity needs to push pull data at various points in its operation not just on start up. But -briefly- to accomplish this I need to fire up another activity to do my Http stuff. :sign0013: but My head really hurts now :BangHead:
 
Last edited:
Upvote 0

lagore

Active Member
Licensed User
Longtime User
If I can give an example of whats in my app, it is composed of 6 'Activity modules' the HttpUtils code module and the HttpUtilsService module. of my activity modules 2 use the HttpUtils, they are "Main" and "DisplayRoster".
Both "Main" & "DisplayRoster" have subs called "UrlDone(Url As String)" & "JobDone (Job As String)". When the app runs in main "HttpUtils.CallbackActivity = "Main"" is set and the HTTP returns are directed from HttpUtils to "Main" and on to 'JobDone' & UrlDone in main for me to process there. Once I am finished in "Main" and "DisplayRoster" activity is launched I re-designate HttpUtils.CallbackActivity = "DisplayRoster" so now when I send a HTTP request HttpUtils will call "DisplayRoster" activity and its subs 'JobDone' & UrlDone' will do the processing. If for some reason I have to re-logon then I re-designate HttpUtils.CallbackActivity = "Main" and re launch "Main"
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Partial Response & Thank You

Thank you lagore I understand your set up. It requires an Activity for the HttpUtil... I appreciate the confirmation that I cannot point the 'WORK' to just a code module....
This is not appropriate for my needs

However from Erel's suggestion I am presently testing the ability to replace the calls to CallSub in the HttpUtilsService to call methods in my Code Modules. I am worried about addressing every instance of CallSub, so that my custom implementation adequately handles whatever the CallSubs were doing initially.

Lastly I am not sure that we have addressed Erel's first suggestion for delegating the events from the activity to the code module.

If we have covered this, then I most assuredly have missed it completely :) :sign0013:



Here's the beginnings of my tests with suggestion #2. As stated I'm worried about making the change to ALL instances of the CallSub2 method call
with my custom method from another code module.

B4X:
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
   finishTasks = finishTasks + 1
   countWorking = countWorking - 1
   If Success = False Then
      HandleError(TaskId, LastException.Message)
   Else
      HttpUtils.SuccessfulUrls.Put(taskToRequest.Get(TaskId), TaskId)
      'Raise URL done event
      If HttpUtils.CallbackUrlDoneSub <> "" Then            
      Log("TaskId int: " & TaskId)
      Log("TaskId Get: " & taskToRequest.Get(TaskId))      
      OtherCM.WereSoooDone( taskToRequest.Get(TaskId))
     'CallSub2(HttpUtils.CallbackActivity, HttpUtils.CallbackUrlDoneSub, taskToRequest.Get(TaskId))
      End If
   End If
   ProcessNextTask
End Sub
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Well yes but

There's only two references to CallSub2 in the HttpUtilisService,

but is OK to just place in my custom method here to my Code module? I'll have to force all of the calls for web service work to go through this single method.

And lastly what about the code in the main Module ::



B4X:
Sub Activity_Resume
   'Check whether a job has finished while the activity was paused.
   'If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

I just point this also to my custom module and MY Job Done method...

??

Thanks Erel..
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but is OK to just place in my custom method here to my Code module?
Yes.

The code in Activity_Resume is not needed. Code modules do not have a life cycle of their own (as activities and services). They run in the context of the caller. In this case it is the service. This means that the code will always run.
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Sorry & Thank you Soo Much AGAIN

Sory to bother you with this but As I was portin this test over to my production project I did something to Break the ""link''' to the Sax Parser...

So it hits the parser point in Module OtherCM Sub WereSoooDone parser.Parse(inStrm, "Parser") at line 51 but it never goes to either of the sax parser methods..

Can you tell whats wrong ?

Thank you
 

Attachments

  • Broken.zip
    11.1 KB · Views: 223
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
aliased methods

These are referenced in the Service

they are checked before the specific method that process the
response from the web service. If I'm using a code module are these
no longer declared in the activity and thusly not validated in the service


HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.CallbackUrlDoneSub = "UrlDone"

Since I'm manually replacing this CallBack Activity with a sub in MY ode module it seems like the alias to the call back subs
should be removed also ..

HttpUtils.CallbackActivity = "Main"


Or do they all get up dated to point to my code module and its sub ??

B4X:
If HttpUtils.CallbackUrlDoneSub <> "" Then      
         OtherCM.WereSoooDone(taskToRequest.Get(TaskId))
    'CallSub2(HttpUtils.CallbackActivity, HttpUtils.CallbackUrlDoneSub, taskToRequest.Get(TaskId))                  
      End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
About the parser issue. Code modules cannot directly handle events. The events are raised in the service or activity that caused the object to be initialized.

In your case you should move parser.Initialize to the JobDone sub and add the two sax subs to the service. You can then delegate the events to the code module.

I'm not 100% convinced that you should use this code module at all. It will be easier to handle the call back methods in an activity or service. It will not require any modification to HttpUtils modules.
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
About the parser issue. Code modules cannot directly handle events. The events are raised in the service or activity that caused the object to be initialized.

In your case you should move parser.Initialize to the JobDone sub and add the two sax subs to the service. You can then delegate the events to the code module.


The JobDone Method is replaced by a method in my Code module sooo...
moving the Sax subs to the Service ... Whoops never Mind The Service containing the SAX subs DID indeed respond to the parser declared and initialized in MY Custom Code Module.. That's Just Weird.. :) But Thanks
 
Upvote 0

fbritop

Well-Known Member
Licensed User
Longtime User
I´ve been struggling arround with HTTPUtils.

I have an application that is working with a local DB. I have a service, that every 2 minutes check for any changes in the DB, if there is at least one change, I have to call the httputils to an URL to update the master database.

The deal, is that the user is moving arround different activities, so I did a timer in a service to check the DB and post to the URL in case it needs.

The problem is that the service cannot be referenced as a call back activity, and I cannot reference any activity because the user is going throught different activities.

Any help in how this could be achieved??

Thanks in advance
 
Upvote 0
Top