makeRequest does not call method(:onReceive) in background / WF

Hi !

i've a problem i can't deal with (as a newbie!)

In a watchFace, i put a background Service to call a Financial web service API (would be the same for weather API).

The BF is ok, makeRequest call the webservice, i have my info in the simu / file -> View HTTP Traffic.

But after the request, onreceive function isn't called.

Andy Idea ?

(i put Sys.println almost everywhere…  !)

thanks !

Marco

PS:

time simulation speeding Don't  have any impact on background time services?

I set time simu to x100 faster, but obliged to wait for 5mn !

(

testing target MK1

Photon 4.8.0

SDK 3.1.6

)

  • post your code for the actual makeWebRequest.

    If you want to trigger the background, in the sim use Simulation>Background Events

  • thanks, i 'll test the simu /BG Events Asap !

    Here is the code 

        function makeRequest(Stock_symbol) {
    Sys.println("in makerequest : " + Stock_symbol);      
          symbol= "&symbol=" + Stock_symbol;
          var params = {};
       var headers = {"Accept" => "application/json" };
          var options = {
        :method => Comm.HTTP_REQUEST_METHOD_GET,
        :headers => headers,
        :responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
       };
       var responseCallback = method(:onReceive);
             w3Api += interval + output_size + symbol + api_key ;
       if (Comm has :makeWebRequest ) {
        Comm.makeWebRequest(w3Api, params, options, responseCallback);
        Sys.println(" Comm has :makeWebrequest , full url = " + w3Api );
       }
       else {
        Comm.makeJsonRequest(w3Api, params, options, responseCallback);
        Sys.println("in Jsaonrequest = " + w3Api );
       }
       
       
         }

    Marco

  • and the onReceive :

         function onReceive(responseCode, data) {
    Sys.println("in onReceive------------------");     
             if (responseCode == 200) {
                 return (data);
             } else {
                 return ("Failed to load\nError: " + responseCode.toString());
             }
         }

  • You want to make use of params and not put everything in the url.  Not sure why that would prevent the callback, outcoile cause an issue.  Here for example, is how I do things when getting weather data, and you see how things like lat,lon, etc are included.

    	    	var UNITS=(Sys.getDeviceSettings().temperatureUnits==Sys.UNIT_STATUTE) ? "imperial" : "metric";
    			var url="https://api.openweathermap.org/data/2.5/weather";
    	    	var param={
    	    		"lat" => latlon[0].toFloat(),
    	    		"lon"=>latlon[1].toFloat(),
    	    		"appid"=>APIKEY,
    	    		"units" => UNITS};
    			var options = {
    				:methods => Comm.HTTP_REQUEST_METHOD_GET,
    				:headers => {"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
    				:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
    			};
       	
    			Comm.makeWebRequest(
    	    	   	url,
    	    	   	param,
    	    	   	options,
    	    	   	method(:receiveWeather));

  • Just 'cos i tried to put in params and so after i wanted to be sure it was not the cause of Something wrong...

    i also tried to put method(:onReceive) instead of responseCallback but no way...

    (just try Simu BG events, i didn't find the app running in target!)

  • Are you sure the background itself is every running?  Do you see any println calls from it in the console?

  • Background: temp in App initialize: 6
    Background: Counter in App initialize: 6
    Background: getServiceDelegate: 19:48
    Background: bg init: 19:48
    Background: onTemporalEvent: 19:48
    Background: in makerequest : GNFT.PA
    Background:  Comm has :makeWebrequest , full url = www.alphavantage.co/query
    Background: onTemporalEvent exit after makeRequest: 19:48 retour web=null
    Background: onStop ...
     on update WF view=5
     on update WF view=6
     on update WF view=7
     on update WF view=8
     on update WF view=9
     on update WF view=10
    Background: temp in App initialize: 6
    Background: Counter in App initialize: 6
    Background: getServiceDelegate: 19:53
    Background: bg init: 19:53
    Background: onTemporalEvent: 19:53
    Background: in makerequest : GNFT.PA
    Background:  Comm has :makeWebrequest , full url = www.alphavantage.co/query
    Background: onTemporalEvent exit after makeRequest: 19:53 retour web=null
    Background: onStop ...
     on update WF view=11
     on update WF view=12
     on update WF view=13
     on update WF view=14
     on update WF view=15
     on update WF view=16
    Background: temp in App initialize: 6
    Background: Counter in App initialize: 6
    Background: getServiceDelegate: 19:58
    Background: bg init: 19:58
    Background: onTemporalEvent: 19:58
    Background: in makerequest : GNFT.PA
    Background:  Comm has :makeWebrequest , full url = www.alphavantage.co/query
    Background: onTemporalEvent exit after makeRequest: 19:58 retour web=null
    Background: onStop ...
     on update WF view=17
     on update WF view=18
     on update WF view=19
     on update WF view=20
     on update WF view=21
     on update WF view=22
    Background: temp in App initialize: 6
    Background: Counter in App initialize: 6
    Background: getServiceDelegate: 20:03
    Background: bg init: 20:03
    Background: onTemporalEvent: 20:03
    Background: in makerequest : GNFT.PA
    Background:  Comm has :makeWebrequest , full url = www.alphavantage.co/query
    Background: onTemporalEvent exit after makeRequest: 20:03 retour web=null
    Background: onStop ...

  • (

    WebStockRequestServiceDelegate is the class in bg :

    " (:background)
    class WebStockRequestServiceDelegate extends Toybox.System.ServiceDelegate
    {

    "

    )

    may it be an error here ?

    function getServiceDelegate(){
         var now=Sys.getClockTime();
         var ts=now.hour+":"+now.min.format("%02d");   
         Sys.println("getServiceDelegate: "+ts);
            return [new WebStockRequestServiceDelegate()];
        }

    or here (end of WebStockRequestServiceDelegate)

        function initialize() {
      Sys.ServiceDelegate.initialize();
      inBackground=true;    //trick for onExit()
    var now=Sys.getClockTime();
    var ts=now.hour+":"+now.min.format("%02d");
    Sys.println("bg init: "+ts);  
      
     }
     
        function onTemporalEvent() {
         var now=Sys.getClockTime();
         var ts=now.hour+":"+now.min.format("%02d");
            Sys.println("onTemporalEvent: "+ts);
           
            //just return the timestamp
    //      var msg_value = makeRequest(Stock_symbol);

          var msg_value ="test";

            makeRequest(Stock_symbol);
    Sys.println("onTemporalEvent exit after makeRequest: "+ts+ " retour web=" + msg_value);       
            Background.exit(msg_value);
        }
          

    ??

    thanks

    (Excuse my poor English language and delay for answering from France, it's 11PM here …St Tropez nearby)

  •         makeRequest(Stock_symbol);
    Sys.println("onTemporalEvent exit after makeRequest: "+ts+ " retour web=" + msg_value);       
            Background.exit(msg_value);

    This is your problem.  You don't want to do the Background.exit() here.  You want to do it in the callback and return the data.

  • Wow ! how to thanks you ? Merci !! So much !!!

    I was pretty sure the callback was immediatly called by makeRequest ( thought they were linked as one and  "unique" function).

    By now, the bg.exit is going to stop the callback, before the return()?

    So i suppose no need anymore of the return, eventually just put a "N/A" string in data if responsecode is != 200?

    or adding a parsing function and put the bg.exit() in...

    It works fine now. I'm going to clean my code...

    thank you

    ;-) 

    Marco

    As you could have seen, i am largely inspire by your work, your examples, and your tutos (like the Guest post about BG, like your app vsbgwf). So thank you very much for all the job you do here (with some others pretty good guys). Thank you for the time you give to all of us in need of such help !