Created
May 25, 2013 21:01
-
-
Save Katharine/5650761 to your computer and use it in GitHub Desktop.
Simplest possible HTTP demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pebble_os.h" | |
#include "pebble_app.h" | |
#include "pebble_fonts.h" | |
#include "http.h" | |
PBL_APP_INFO_SIMPLE(HTTP_UUID, "HTTP Demo", "Demo Corp", 1 /* App version */); | |
Window window; | |
TextLayer textLayer; | |
// Modify these common button handlers | |
void select_single_click_handler(ClickRecognizerRef recognizer, Window *window) { | |
DictionaryIterator* dict; | |
http_out_get("http://pwdb.kathar.in/pebble/test.json", 17, &dict); | |
http_out_send(); | |
text_layer_set_text(&textLayer, "Sending."); | |
} | |
// This usually won't need to be modified | |
void click_config_provider(ClickConfig **config, Window *window) { | |
config[BUTTON_ID_SELECT]->click.handler = (ClickHandler) select_single_click_handler; | |
} | |
void success(int32_t request_id, int http_status, DictionaryIterator* received, void* context) { | |
Tuple* tuple = dict_find(received, 3); | |
if(!tuple) { | |
text_layer_set_text(&textLayer, "Key 3 not found."); | |
return; | |
} | |
text_layer_set_text(&textLayer, tuple->value->cstring); | |
} | |
// Standard app initialisation | |
void handle_init(AppContextRef ctx) { | |
window_init(&window, "Button App"); | |
window_stack_push(&window, true /* Animated */); | |
text_layer_init(&textLayer, window.layer.frame); | |
text_layer_set_text(&textLayer, "Press select"); | |
text_layer_set_font(&textLayer, fonts_get_system_font(FONT_KEY_GOTHAM_30_BLACK)); | |
layer_add_child(&window.layer, &textLayer.layer); | |
// Attach our desired button functionality | |
window_set_click_config_provider(&window, (ClickConfigProvider) click_config_provider); | |
// Set our app ID | |
http_set_app_id(-1780059304); | |
// Set up handlers. | |
http_register_callbacks((HTTPCallbacks){ | |
.success = success | |
}, NULL); | |
} | |
void pbl_main(void *params) { | |
PebbleAppHandlers handlers = { | |
.init_handler = &handle_init, | |
.messaging_info = { | |
.buffer_sizes = { | |
.inbound = 124, | |
.outbound = 256, | |
} | |
} | |
}; | |
app_event_loop(params, &handlers); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment