Garmin MonkeyC Communications.makeWebRequest stopped working when moved to SDK 4.2.4

371 views Asked by At

Im stuck and looking for help with this error:

ERROR: fenix6: C:\....\OtherView.mc:90,8: Invalid '$.Toybox.Lang.Method(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary) as Void' passed as parameter 4 of type 'PolyType<(callback(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String) as Void) or (callback(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String, context as $.Toybox.Lang.Object) as Void)>'.

When doing:

var responseCallback = method(:onReceive);
Communications.makeWebRequest(url, params, options, responseCallback);

It was working fine in previous SDK. It seems like callback function is not of the right type anymore, but I'm, not able to find any other 'callback' type in documentation, that could match.

This is the code:

class HttpRequest {
    protected var onAfterReceive;
    public function initialize(onAfterReceiveArg) {
        onAfterReceive = onAfterReceiveArg;
    }
    
    function onReceive(responseCode as Number, data as Dictionary?) as Void {
        if (responseCode == 200) {
            Ui.popView(Ui.SLIDE_IMMEDIATE);
            System.println("Request Successful");
            Extensions.setPropertyAndStorage("test", data["test"]);
            Extensions.setPropertyAndStorage("test1", data["test1"] + "/" + data["test2"]);
            self.onAfterReceive.invoke();
        } else {
            Ui.popView(Ui.SLIDE_IMMEDIATE);
            System.println("Response: " + responseCode);
            self.onAfterReceive.invoke("Can't connect:" + responseCode + "\n" + Extensions.getPropertyOrStorage("test1"));
        }        
    }

    function makeRequest() as Void {
        Ui.pushView( new LoadingView(), null, Ui.SLIDE_IMMEDIATE);
        var url = "https://removed";
        var app = Application.getApp();
        
        var params = {
            //"definedParams" => "123456789abcdefg"
        };
        var options = {
            :method => Communications.HTTP_REQUEST_METHOD_GET,
            :headers => {
                "Authorization" => app.getProperty("apikey"),
                "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED
            },
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
        };

        var responseCallback = method(:onReceive);
        Communications.makeWebRequest(url, params, options, responseCallback);   // ERROR
    }
}

1

There are 1 answers

0
jarekj9 On

Ok, what fixed this issue was to specify arguments types exactly as in the error:

function onReceive(responseCode as Number, data as Null or Dictionary or String) as Void { 
   ...
}

Im not fluent in Monkey C, nevertheless its strange for me, that example does not say anything about this (at https://developer.garmin.com/connect-iq/api-docs/Toybox/Communications.html#makeWebRequest-instance_function)