Can a WatchFace Make Several WebRequests?

Former Member
Former Member
I can successfully make one WebRequest using a WatchFace, but I'd like to make *several* WebRequests. Is it possible? Here is what I have tried so far (unsuccessfully):
1) In onTemporalEvent, I made several calls to makeWebRequest specifying the same onRecieve callback.
2) In onTemporalEvent, I made several calls to makeWebRequest specifying *different* onRecieve callbacks.
3) In getServiceDelegate, I returned an array of several ServiceDelegate instances.
  • Former Member
    Former Member over 7 years ago
    1 and 2 should work if constructed properly (and your background process doesn't run out of time before they complete). You will need to make sure you do not call Background.exit() until all requests have completed, and you have packaged all the data up into your exit payload.

    Another option would be to chain the requests by initiating each request from the callback of the previous request, and exiting from the final one. Your process has a maximum of 30 seconds to complete before it will be terminated by the system, so there will be a functional limit on how many requests you can complete.
  • Another thing to keep in mind is the amount of memory available to the background is less than the watch face itself, (32k if I recall), so you want to be careful as to the sizes of the responses and manage that.

    Also, bear in mind a request is likely much faster in the sim than on a watch.
  • Success?! I also need several Request and always end up with errors...

    thanks for your answer

  • you have to be aware that the callbacks are asynchronously and thus are not in guaranteed order (some may actually never end up in the callback), so before you force an exit from the background you either have to count the amount of callbacks received (and force exit when the amount matches the number of requests) or alternatively you can also chain the webrequests (call the 2nd webrequest from your 1st callback). 

    Personally I prefer method 1 (counting), but you can try both approaches and see what suits you best.

  • The best way to do this is serially.  Make request 1, and in the callback for that, make request 2 and so on.  Doing them all at once can cause issues.  There's a max of 3 outstanding requests, but android doesn't handle that well.

  • I got problems with the order of all the steps. At the end theres one data package i will send to the main function(onbackgrounddata). Thats a bit too complicated for me right now...

    Maybe one of you pros can text me an example of the basic code construction!? I‘ve spent a lot of time till today with that problem.

    thanks so far

  • class MyServiceDelegate extends Sys.ServiceDelegate {
      var res1=null,res2=null;
      
      function onTemporalEvent() {
        ...
      	Comm.makeWebRequest(url1,param1,options1,method(:onReceive1));
        ...
      }
      
      function onReceive1(responseCode, data) {
        res1=data;
        ...
        Comm.makeWebRequest(url2,param2,options2,method(:onReceive2));
        ...
      }
      
      function onReceive2(responseCode, data) {
        res2=data;
        ...
        Background.exit([res1,res2]);
      }

    lots missing here like what you do with errors, but the basics.

  • I will test it as soon as possible. Thank you very much!

  • Check! Thanks for your help!

  • This seems to work fine @jim_m_58 - however, I return in res1 and res2 HTTP_RESPONSE_CONTENT_TYPE_JSON, so it will contain one object res1 in JSON and the second one res2 in JSON. How can I concatenate these objects correctly (like res1+res2)?
    I would like to store the values in  onBackgroundData(data)

    function onBackgroundData(data) {
    	var pendingWebRequests = getPendingWebRequest();	
    	var type = data.keys()[0]; // Type of received data.
    	var storedData = getProperty(type);
    	var receivedData = data[type]; // The actual data received: strip away type key.
    	// New data received: clear pendingWebRequests flag and overwrite stored data.
    	storedData = receivedData;	
    	pendingWebRequests.remove(type);
    	setProperty("PendingWebRequests", pendingWebRequests);
    	setProperty(type, storedData);
    	Ui.requestUpdate();
    }

    I call in OnReceive2

    Bg.exit({"OpenWeatherMapCurrent" => responseCode != 200 ? { "httpError" => responseCode } : [res1, res2]});

    Any hints or clues?