responseCode != 200, weatherData symbol "--"?

Good day, I want to ask how to easily solve when I don't get the requested data, that means responseCode = 200, so e.g. will I have the symbol "--" for the current temperature? I'm currently doing it this way, but I have no idea if it's correct for watch memory for example?

Thanks for the advice and tips.

private var _temp  = "--" as String;

function OWM_Update(responseCode as Number, data as Dictionary or String or Null) as Void {
	
    if (responseCode != 200) 
    {
      System.println("NO WEATHER DATA");
      backgroundData["Temp"] = _temp;
	  
	  Background.exit(backgroundData);
    } else 
    {
    System.println("WEATHER DATA OK");
    backgroundData["Temp"] = ((data as Dictionary)["main"] as Dictionary)["temp"]as Number;
      
   Background.exit(backgroundData);
    }
     }

  • Add a println to onBackgroundData to see the data it's passed.

    It's easy to force an error in the sim by going to "Settings>Connection Type>BLE" and setting it to "Not Connected"

    I don't see where you are using "data" or where you are setting "backgroundData".  For a non-200, data will likely be null and if that's returned in Background.exit(), onBackgroundData won't be called.

  • For me, it's about the fact that if "BLE" is not connected, for example, I want the value "--" to be returned to me on the data. The question was, is this solution correct?

    Here is the code for onBackgroundData(data):

  • function onBackgroundData(data) {

    var weather = data as Dictionary?;

    var temperature = weather["main"] as Dictionary;

    if (data != null) {
    Application.Storage.setValue("temperature", temperature["temp"] as Numeric);
    }
    }

    }

  • What do you see if you add 

    System.println(data);

    at the very top of onBackgroundData?

  • i see this:

    System.println("Synchronize "+ dataInput);
    Background: getServiceDelegate: 13:34
    Background: WEATHER DATA OK
    Synchronize {Temp=>-8.990000}

    As I wrote, they need to make it so that if I don't get (responseCode == 200), then on the next refresh: onBackgroundData(data) will appear next to the data on my watch, for example the symbol "- - " And I wonder if it should have done this well, or is there something simpler?

  • Here is what I dp for OWM in a background service/

    In the callback for the makeWebRequest, I have this:

    		function receiveWeather(responseCode, data) {
    			if(responseCode==200) {
    				Background.exit(data);
    			}
    			else {Background.exit(responseCode);}
    		}

    Then in the main app, onBackgroundData looks like this:

        function onBackgroundData(data) {
        	if(data instanceof Number) {
        		commStat=data;
        	} else {
            	commStat=200;
            	Application.Storage.setValue("OWMData", data);
            }
            Application.Storage.setValue("OWMStatus, commStat);
            
            WatchUi.requestUpdate();
        }

  • Hi, thanks for the example, but when I try to import it and modify it according to my code, it doesn't work very well. Could you please make a sample so I know where to put the weather data and how to call it?

    Thank you

    function receiveWeather(responseCode, data) {
    			if(responseCode==200) {
    				Background.exit(data);
    			}
    			else {Background.exit(responseCode);}
    		}
    		
    
    
    
    where should I put, for example, this?
    
    "temp" => data["main"]["temp"],
    
    OR 
    
    _rec["Temp"] = ((data as Dictionary)["main"] as Dictionary)["temp"]as Number;

    what is meant by OWMStatus ?

  • "OWMStatus" is the key used by Storage.setValue for how I save the status.

    Again, just put a println at the beginning of onBackgroundData.  Don't include your code in receiveWeather.

    Then in the watchface, you decide what to do if the status wasn't 200.  You may want to show the last known temperature in red instead of "--" for example.

  • so should I understand that I will call the weather data via function onBackgroundData(data)?

  • That's now you get the weather data from the background service.