Issues with JsonRequest

Former Member
Former Member
Hi guys, I'm trying to do my first widget for the Garmin IQ but I'm have some issues with the JsonRequest function.
Here is my code below :
using Toybox.Communications as Comm;
using Toybox.WatchUi as Ui;
using Toybox.Graphics;
using Toybox.System as Sys;
using Toybox.Position;

class Stocks
{
var symb;
var Bid;
}


class StocksView extends Ui.View {
hidden var mStocks = "";

//! Load your resources here
function onLayout(dc) {

mStocks = "Waiting for Stocks Data";

}

//! Restore the state of the app and prepare the view to be shown
function onShow() {
}

//! Update the view
function onUpdate(dc) {

dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);
dc.clear();
dc.drawText(dc.getWidth()/2, dc.getHeight()/2, Graphics.FONT_MEDIUM, mStocks, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER);

Comm.makeJsonRequest("query.yahooapis.com/.../yql",
{"q"=>"select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22SRPT%22)%0A%09%09", "format"=>"json","diagnostics"=>"true","env"=>"store%3A%2F%2Fdatatables.org%2Falltableswithkeys"}, {}, method(:onReceive));

}

//! Called when this View is removed from the screen. Save the
//! state of your app here.
function onHide() {
}

function onReceive(responseCode, data)
{
if( responseCode == 200 )
{
var stocks = new Stocks();
weather.Symb = data["symbol"];
weather.Bid = data["Bid"];
Sys.println(weather.Symb);
Sys.println(weather.Bid);
}
else
{
mStocks = "Failed to load\nError: " + responseCode.toString();


}
Ui.requestUpdate();
}

}


I receive an error code 404 from the JSON request.

The original call to the api is
query.yahooapis.com/.../yql

Any help will be much appreciated :-)
  • Former Member
    Former Member over 10 years ago
    From what I managed to dig out of previous posts in the forum, https doesn't work in the simulator, but does on a device.
  • Former Member
    Former Member over 10 years ago
    From my own experience the url is probably too long
  • Former Member
    Former Member over 10 years ago
    oh ok I see, I'll try on the device and if it's not working I'll try a different API with a shorter URL.
    Will let you know.
  • From my own experience the url is probably too long


    I have some much longer URLs in My Quote widget. Here I have an URL that is ~500 characters long.

    I believe https is the problem.
  • Former Member
    Former Member over 10 years ago
    I have some much longer URLs in My Quote widget. Here I have an URL that is ~500 characters long.

    I believe https is the problem.
    In the request?
  • Former Member
    Former Member over 10 years ago
    oh ok I see, I'll try on the device and if it's not working I'll try a different API with a shorter URL.
    Will let you know.
    http://www.jsontest.com/ is an easy way to test if the basic features are working.
  • In the request?


    Yep. The URL is approx. 500 characters
  • Former Member
    Former Member over 10 years ago
    Lol,. Oh, by URL you meant URL. Doh.
  • Former Member
    Former Member over 9 years ago
    json request over https does not work

    Same thing here. I also keep getting 404 responses. The json service I'm calling is https only..
    Did you ever find out if this is a emulator limitation only? Does https work on the device?

    Thanks

    Hi guys, I'm trying to do my first widget for the Garmin IQ but I'm have some issues with the JsonRequest function.
    Here is my code below :
    using Toybox.Communications as Comm;
    using Toybox.WatchUi as Ui;
    using Toybox.Graphics;
    using Toybox.System as Sys;
    using Toybox.Position;

    class Stocks
    {
    var symb;
    var Bid;
    }


    class StocksView extends Ui.View {
    hidden var mStocks = "";

    //! Load your resources here
    function onLayout(dc) {

    mStocks = "Waiting for Stocks Data";

    }

    //! Restore the state of the app and prepare the view to be shown
    function onShow() {
    }

    //! Update the view
    function onUpdate(dc) {

    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);
    dc.clear();
    dc.drawText(dc.getWidth()/2, dc.getHeight()/2, Graphics.FONT_MEDIUM, mStocks, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER);

    Comm.makeJsonRequest("query.yahooapis.com/.../yql",
    {"q"=>"select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22SRPT%22)%0A%09%09", "format"=>"json","diagnostics"=>"true","env"=>"store%3A%2F%2Fdatatables.org%2Falltableswithkeys"}, {}, method(:onReceive));

    }

    //! Called when this View is removed from the screen. Save the
    //! state of your app here.
    function onHide() {
    }

    function onReceive(responseCode, data)
    {
    if( responseCode == 200 )
    {
    var stocks = new Stocks();
    weather.Symb = data["symbol"];
    weather.Bid = data["Bid"];
    Sys.println(weather.Symb);
    Sys.println(weather.Bid);
    }
    else
    {
    mStocks = "Failed to load\nError: " + responseCode.toString();


    }
    Ui.requestUpdate();
    }

    }


    I receive an error code 404 from the JSON request.

    The original call to the api is
    query.yahooapis.com/.../yql

    Any help will be much appreciated :-)
  • I think one of the things fixed in sdk 1.1.2 is the https in the simulator.

    Also, I wouldn't do the makeJsonRequest in onUpdate. If it's an app, I'd do it based on a timer, after an initial request in either onShow or initialize.

    Then in onReceive, call Ui.requestUpdate() to display the data.

    With onUpdate() doing the makeJson, as soon as onReceive is done, onUpdate is called and anouther makeJson is being fired off right away, even if there is already one in progress from another call to onUpdate.