-
-
Save mwbrooks/473585 to your computer and use it in GitHub Desktop.
<!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> |
Thanks for sharing! I have tried the code and it works just fine in the simulator bot not on the device :(
We have a BlackBerry Curve 8520 with OS 4.6.1
On which device/OS have you tested?
Regards
Björn
Björn,
I tested the code on a BlackBerry Bold 9000 running OS 4.6.0 and built the app using the 4.6.1 component pack.
Do you have code signing keys setup? In order to run RIM Java class code on a device, you must sign the application using RIM code signing keys. You don't have to do this for the simulator.
Michael
Hi Michael,
yes I have code signing keys to run the App on my BlackBerry.
Hve you put it on the device by selecting "Debug as..." in Eclipse or by installing it through the Desktop Manager (can't imagine that this may be the issue, but you never know). Sorry for all those questions but I'm not very familiar with BlackBerry ;-)
Thanks for taking the time to answer my questions :)
Björn,
Too bad about the code signing keys - that would have been an easy fix ;)
I've deployed the app using the Debug as... in Eclipse and the PhoneGap-BlackBerry ANT script ant load-device
. I haven't used the Desktop Manager for this app.
I'm not an experienced BlackBerry developer either, but I've quickly learned that this is a issue that comes up too often. I asked filmaj to check over the code and - at a glance - he thinks it looks fine.
Would you mind dropping a break point in Java to see what is really going on? Around line 198 of NetworkCommand.java. While debugging, your app will crash but the debugging will continue running. This is normal - the phone closes the app because it thinks the app has stopped responding.
I'll try that and come back to you as I have results. I also found out that somehow the component pack for 4.6.1 wasn't installed into Eclipse anymore. Just the 5.0 maybe that was causing the issue...
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
Brijrajv,
The server code is just a simple Sinatra server:
require 'rubygems'
require 'sinatra'
get '/' do
name = params[:name] || 'Michael'
"{ 'name': '#{name}' }"
end
What is wrong with POST?