B4J Question ABPlugin with ResumableSub

Blueforcer

Well-Known Member
Licensed User
Longtime User
I use ABPlugin from @alwaysbusy for my application. Its an awesome Lib btw!
One Plugin has to download something and parse the response to the Main Application.

because i need to use
B4X:
Wait For (j) JobDone(j As HttpJob)
to wait for the response, the Run sub has to be a ResumableSub to parse the response.


B4X:
public Sub Run(Tag As String, Params As Map) As ResumableSub

    Select Case Tag
 
        Case "tick"
            Dim j As HttpJob
            j.Initialize("job",Me)
            j.Download("http://api.apixu.com/v1/current.json")
            Wait For (j) JobDone(j As HttpJob)
            Dim parser As JSONParser
            parser.Initialize(j.GetString)
            Dim root As Map = parser.NextObject
            Dim current As Map = root.Get("current")
            Dim condition As Map = current.Get("condition")
            Dim icon As String = condition.Get("icon")
            Dim temp As String = current.Get("temp_c")
            Return  CreateMap("URLicon":Array As Object("http:"&icon))
    End Select
    Return ""
End Sub

But i think the lib doesnt support it?
In my main application i try to get the map with
B4X:
Dim rs As ResumableSub = plugin.RunPlugin("test"), "start", CreateMap("":Null))
    Wait For(rs) Complete (Result As Map)
    For Each k As String In Result.Keys

        next
but i get the error:
B4X:
java.lang.ClassCastException: anywheresoftware.b4a.keywords.Common$ResumableSubWrapper cannot be cast to anywheresoftware.b4a.BA$ResumableSub

how can i make this work?
 
Last edited:

Blueforcer

Well-Known Member
Licensed User
Longtime User
Well this fix caused no error
B4X:
Wait For(plugin.RunPlugin(apps.Get(activeApp), "tick", Null)) Complete (Result As Map)

but the jobDone event inside the plugin never fired.
if i compile the plugin and use it standalone it works but not loaded as plugin in my mainapplication.
Plugin and Mainapplication are Non-UI
 
Last edited:
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
That's too bad. I wish I had used your Lib for such purposes.
Hopefully there is someone who can fix this problem.
Or do you know another way to download something in a plugin?
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Actually , I think ABPlugin does work with ResumableSub, but something must be wrong in your code. If I try your url, I get an error from the REST API that the API key has not been provided, so I used another URL.

Note: to let this work, I did have to also include the jHttpUtils and Json libraries in the Calling Program too!

Plugin code:
B4X:
' class is called HttpJobPlugin
Sub Class_Globals
   
End Sub

'Initializes the object. You can NOT add parameters to this method!
Public Sub Initialize() As String
   Log("Initializing plugin " & GetNiceName)
   ' Here return a key to prevent running unauthorized plugins
   Return "MyKey"
End Sub

' must be available
public Sub GetNiceName() As String
   Return "HttpJob"
End Sub

' must be available
public Sub Run(Tag As String, Params As Map) As ResumableSub
   Select Case Tag
       Case "tick"
           Dim j As HttpJob
           j.Initialize("job",Me)
           j.Download("https://reqres.in/api/users?page=1")
           Wait For (j) JobDone(j As HttpJob)
           Dim parser As JSONParser
           parser.Initialize(j.GetString)
           Dim root As Map = parser.NextObject
           Dim page As String = root.Get("page")
           Return CreateMap("Return": "Page = " & page)           
   End Select
   Return CreateMap("Return": "UNKNOWN")
End Sub

Calling Program:
B4X:
Sub Button2_Click
   Dim params As Map
   params.Initialize
   Wait For(plugin.RunPlugin("HttpJob", "tick", Null)) Complete (Result As Map)
   Log("Return value: " & Result.Get("Return"))
End Sub

Result:
B4X:
Return value: Page = 1
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
I think there is a problem with non-gui apps. becaue i cant use jHttpUtils i need to use jOkHttpUtils2_NONUI. But with this there is no response inside the plugin.
Same code with jHttpUtils and UI App works
 
Last edited:
Upvote 0

woniol

Active Member
Licensed User
Longtime User
Hi, I tried it as well.
Demo plugins work ok, but when I recompile the HttpJobPlugin and upload it to the plugins dir of the main program,
It's not loading...
Can you please check the source from the ABPlugin1.20.zip

[Solved]
I forgot about 'compile to library'. Now it works fine.
Thanks
 
Last edited:
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
So as i understand its not possible for a NonGUI app to use Plugins wich can download something?
Do you know another way to fit my needs?
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Whilst not related to your issue...
I use this simple Sub to return a map from the job.GetString

B4X:
'convert a json 2 map
private Sub Json2Map(jsonText As String) As Map
    Dim json As JSONParser
    Dim Map1 As Map
    Map1.Initialize
    Try
        If jsonText.length > 0 Then
            json.Initialize(jsonText)
            Map1 = json.NextObject
        End If
        Return Map1
    Catch
        Return Map1
    End Try
End Sub

Thus will have something like

B4X:
Dim root As Map = Json2Map(j.GetString)
Dim icon as string = root.getdefault("icon","")

From your code, instead of using

B4X:
Return  CreateMap("URLicon":Array As Object("http:"&icon))

Why don't you just perhaps use (as icon variable is a string)?

B4X:
Return  CreateMap("URLicon": $"http:${icon}"$)

#Still2Explore ABMPlugIn
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
tried different things to make this all work in a nonUI application. Sadly without any success :(
So i have to switch to a GUI app (without a gui and even without any jFX functions), which excludes distributions without desktop and makes it tricky to start the app remotetly via SHH. Also I can't provide with webserver functionality.

if someone has a great idea for my problem I would be very grateful
 
Last edited:
Upvote 0
Top