Created
July 13, 2010 07:44
-
-
Save mwbrooks/473585 to your computer and use it in GitHub Desktop.
PhoneGap-BlackBerry XHR
This file contains hidden or 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
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
| "http://www.w3.org/TR/html4/strict.dtd"> | |
| <html> | |
| <head> | |
| <title>BlackBerry XHR Example</title> | |
| <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> | |
| <script type="text/javascript" charset="utf-8"> | |
| // Call onDeviceReady when PhoneGap is loaded. | |
| // | |
| function onLoad() { | |
| // BlackBerry OS 4 browser does not support events. | |
| // So, manually wait until PhoneGap is available. | |
| // | |
| var intervalID = window.setInterval( | |
| function() { | |
| if (PhoneGap.available) { | |
| window.clearInterval(intervalID); | |
| onDeviceReady(); | |
| } | |
| }, | |
| 500 | |
| ); | |
| } | |
| // PhoneGap is loaded and it is now safe to make calls PhoneGap methods | |
| // | |
| function onDeviceReady() { | |
| document.getElementById('phonegapState').innerHTML = 'loaded and ready to rock'; | |
| } | |
| // XHR GET Request | |
| // | |
| function get() { | |
| var url = 'http://michaelbrooks.ca:4568/?name=Mikey'; | |
| // PhoneGap-BlackBerry returns the responseText as the argument | |
| // At the moment, the BlackBerry XHR only supports JSON formatted responses. | |
| // It is easy to extend PhoneGap-BlackBerry to accept regular strings, but | |
| // that is not the case currently. | |
| // | |
| var requestCallback = function(response) { | |
| // response value: { 'name': 'Mikey' } | |
| // | |
| alert('Name is ' + response['name']); | |
| }; | |
| navigator.network.XHR(url, null, requestCallback); | |
| } | |
| // XHR POST Request | |
| // (This code does not work) | |
| // | |
| function post() { | |
| var url = 'http://michaelbrooks.ca:4568/'; | |
| var requestCallback = function(response) { | |
| alert('Place is ' + response['place']); | |
| }; | |
| navigator.network.XHR(url, 'place=Germany', requestCallback); | |
| } | |
| </script> | |
| </head> | |
| <body onload="onLoad();"> | |
| <p>PhoneGap is <span id="phonegapState">not loaded yet.</span>.</p> | |
| <p><a href="#" onclick="get(); return false;">Send a GET Request</a></p> | |
| <p><a href="#" onclick="post(); return false;">Send a POST Request</a></p> | |
| </body> | |
| </html> |
Author
Brijrajv,
The server code is just a simple Sinatra server:
require 'rubygems'
require 'sinatra'
get '/' do
name = params[:name] || 'Michael'
"{ 'name': '#{name}' }"
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Can you share the server code if you don't mind. The code we wrote sends the JSON text wrapped in XML tags. The device side code is not able parse thereafter.
Thanks,
Brijraj