Need help making a button

I am new to programming and would like to make a Widget with a button that when pressed triggers an HTTP Get request. Could someone point me in the right direction as to what is needed to make this happen. My plan is to have it send an http get request to io.adafruit.com to change a value on my feed.

Thanks
  • Sorry for the late reply. I was able to get it working late last night, but was too tired to post about it. I found that my main issue was that I was not setting the InputDelegate in "function getInitialView()" under class HTTPApp extends App.AppBase as TeunMo suggested. I found that both InputDelegate and BehaviorDelegate worked after that. I was able to test it with my car today and got it to lock and unlock just fine.
    Here is a post I made about the car and microcontroller.

    This is my code as of now. It still needs improvement, but it works!


    using Toybox.WatchUi as Ui;
    using Toybox.Application as App;
    using Toybox.Graphics as Gfx;
    using Toybox.System as Sys;
    using Toybox.Communications as Comm;
    var clearScreen = false;

    class HTTPView extends Ui.View {

    function initialize() {
    View.initialize();
    }

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

    }

    //! Called when this View is brought to the foreground. Restore
    //! the state of this View and prepare it to be shown. This includes
    //! loading resources into memory.
    function onShow() {
    }

    //! Update the view
    function onUpdate(dc) {
    // Call the parent onUpdate function to redraw the layout
    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT);
    dc.fillRectangle(0, 0, dc.getWidth(), dc.getHeight());
    dc.setColor(Gfx.COLOR_BLUE, Gfx.COLOR_WHITE);
    dc.fillRectangle(0, 0, 208*.333, 150*.333);
    dc.setColor(Gfx.COLOR_YELLOW, Gfx.COLOR_WHITE);
    dc.fillRectangle(208*.66, 150*.66, 208*.333, 150*.333);
    dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_TRANSPARENT);
    dc.drawText((dc.getWidth() * .165), ((dc.getHeight() * .165) -10), Gfx.FONT_SMALL, "LOCK", Gfx.TEXT_JUSTIFY_CENTER);
    dc.drawText((dc.getWidth() * .825), ((dc.getHeight() * .825) -10), Gfx.FONT_SMALL, "UNLOCK", Gfx.TEXT_JUSTIFY_CENTER);
    if(clearScreen == true)
    {
    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT);
    dc.fillRectangle(0, 0, dc.getWidth(), dc.getHeight());
    dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_WHITE);
    dc.drawText((dc.getWidth() / 2), ((dc.getHeight() / 2) - 30), Gfx.FONT_SMALL, "Request Sent!!!", Gfx.TEXT_JUSTIFY_CENTER);
    clearScreen = false;
    }


    }

    //! Called when this View is removed from the screen. Save the
    //! state of this View here. This includes freeing resources from
    //! memory.
    function onHide() {
    }

    }

    class HTTPDelegate extends Ui.InputDelegate
    {
    function onTap(evt)
    {

    var xy = evt.getCoordinates();
    if(xy[0]>0 && xy[0]<(208*.33) && xy[1]>0 && xy[1]<(150*.33))
    {
    Comm.makeJsonRequest("io.adafruit.com/.../send.json
    clearScreen = true;
    Ui.requestUpdate();

    }

    if(xy[0]>(208*.66) && xy[0]<(208) && xy[1]>(150*.66) && xy[1]<(150))
    {
    Comm.makeJsonRequest("io.adafruit.com/.../send.json
    clearScreen = true;
    Ui.requestUpdate();

    }

    }
    }


    Thank you jim_m_58, Travis, and TeunMo for all your help.
  • I got your button example working Travis.  works very well.  there are a couple small changes to get it to work but nothing too complicated...

    What i fail to understand i guess, mainly because i am a novice at best, is how do you call more than one button ?

    the App returns a [View, Delegate]... which i understand, and creates a new registry.

    where would you add a call for additional buttons here?

    i started off by passing an array to the View, and i was able to get it to work, creating two buttons in my case.  I feel this is the wrong way! i can get onTap to work, but it only seems work for the last button...   There must be a better way to call multiple buttons.

    Thank you!