[PhoneGap] Ajax is not working in Black Berry

34 views
Skip to first unread message

Udham Singh

unread,
Apr 19, 2010, 12:36:42 AM4/19/10
to phonegap
Hello,

I am creating html project with ajax. When i deploy the
project with phonegap-android app in Android environment, it just
working fine, but when i deploy the project with phonegap-Blackberry
app in Blackberry environment(Modele above Bold 9000) the ajax is not
working ,

I followed the below link, how can i make xhr call. Can any one
provide example for making ajax in blackberry mobile.

http://groups.google.com/group/phonegap/browse_thread/thread/2bbe89c6e5ca6ca5/50bb52d15fe1fe1f?lnk=gst&q=BlackBerry+ajax#50bb52d15fe1fe1f.


I used below ajax code

function showCustomer()
{
var url="getcustomer.asp";


if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=stateChange;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=stateChange;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
}


How can i make this code working in BlackBerry


Thanks
Udham

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Jesse MacFadyen

unread,
Apr 19, 2010, 12:46:19 AM4/19/10
to Udham Singh, phonegap
As it says in known issues:

Native XHRs do not work on PhoneGap BlackBerry. You can have a look at the Java-based, native implementation of XHR that we've implemented here: http://github.com/phonegap/phonegap-blackberry/blob/master/framework/src/com/nitobi/phonegap/api/impl/NetworkCommand.javaThe JavaScript interface looks like this:
navigator.network.XHR(url, postData, callback);
url - string parameter of the URL to send the request to.
postData - string parameter of POST data to send (can be null if you don't want to send POST - just add GET parameters as querystring to the URL parameter if that's the case).
callback - a function handle to execute when the XHR returns.

So you need to call:
navigator.network.XHR(url, postData, callback);
Instead of:
 xmlhttp=new XMLHttpRequest();
 xmlhttp.onreadystatechange=stateChange;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);

Sent from my iPhone

Udham Singh

unread,
Apr 19, 2010, 1:28:42 AM4/19/10
to phonegap
Thank you for your quick reply.

Can you please help me whether my code is correct or not.

my code is


function showCustomer(strVal)
{

var url="http://mydomain.com/getXml.asp";
navigator.network.XHR(url, "Get", stateChanged);

}

function stateChanged()
{

How can i get the respose as XML data here.

}




It says "navigator.network" is null or not Object error.
and How can i get the respose as XML data


I am new to ajax and Javascript and also phonegap, sorry if my
questions are weird you.

Thanks
Udham




On Apr 19, 9:46 am, Jesse MacFadyen <jesse.macfad...@nitobi.com>
wrote:
> As it says in known issues:
>
> Native XHRs do not work on PhoneGap BlackBerry. You can have a look at  
> the Java-based, native implementation of XHR that we've implemented  
> here:http://github.com/phonegap/phonegap-blackberry/blob/master/framework/...
>   JavaScript interface looks like this:
> navigator.network.XHR(url, postData, callback);
> url - string parameter of the URL to send the request to.
> postData - string parameter of POST data to send (can be null if you  
> don't want to send POST - just add GET parameters as querystring to  
> the URL parameter if that's the case).
> callback - a function handle to execute when the XHR returns.
>
> So you need to call:
> navigator.network.XHR(url, postData, callback);
> Instead of:
>
> >  xmlhttp=new XMLHttpRequest();
> >  xmlhttp.onreadystatechange=stateChange;
> >  xmlhttp.open("GET",url,true);
> >  xmlhttp.send(null);
>
> Sent from my iPhone
>
> On 2010-04-18, at 9:36 PM, Udham Singh <udham1...@gmail.com> wrote:
>
>
>
>
>
> > Hello,
>
> >         I am creating html project with ajax. When i deploy the
> > project with phonegap-android app in Android environment, it just
> > working fine, but when i deploy the project with phonegap-Blackberry
> > app in Blackberry environment(Modele above Bold 9000) the ajax is not
> > working ,
>
> >       I followed the below link, how can i make xhr call. Can any one
> > provide example for making ajax in blackberry mobile.
>
> >http://groups.google.com/group/phonegap/browse_thread/thread/2bbe89c6...
> > .
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en

azayed

unread,
Apr 19, 2010, 1:22:14 PM4/19/10
to phonegap
I´d love to ask the same question too..


Could you please give use a practicle example of how to use it?

I would really appreciate it so much...

Thank you in advance

On 19 abr, 06:46, Jesse MacFadyen <jesse.macfad...@nitobi.com> wrote:
> As it says in known issues:
>
> Native XHRs do not work on PhoneGap BlackBerry. You can have a look at  
> the Java-based, native implementation of XHR that we've implemented  
> here:http://github.com/phonegap/phonegap-blackberry/blob/master/framework/...
>   JavaScript interface looks like this:
> navigator.network.XHR(url, postData, callback);
> url - string parameter of the URL to send the request to.
> postData - string parameter of POST data to send (can be null if you  
> don't want to send POST - just add GET parameters as querystring to  
> the URL parameter if that's the case).
> callback - a function handle to execute when the XHR returns.
>
> So you need to call:
> navigator.network.XHR(url, postData, callback);
> Instead of:
>
> >  xmlhttp=new XMLHttpRequest();
> >  xmlhttp.onreadystatechange=stateChange;
> >  xmlhttp.open("GET",url,true);
> >  xmlhttp.send(null);
>
> Sent from my iPhone
>
> On 2010-04-18, at 9:36 PM, Udham Singh <udham1...@gmail.com> wrote:
>
>
>
>
>
> > Hello,
>
> >         I am creating html project with ajax. When i deploy the
> > project with phonegap-android app in Android environment, it just
> > working fine, but when i deploy the project with phonegap-Blackberry
> > app in Blackberry environment(Modele above Bold 9000) the ajax is not
> > working ,
>
> >       I followed the below link, how can i make xhr call. Can any one
> > provide example for making ajax in blackberry mobile.
>
> >http://groups.google.com/group/phonegap/browse_thread/thread/2bbe89c6...
> > .
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en

filmaj

unread,
Apr 19, 2010, 4:02:38 PM4/19/10
to phonegap
If 'navigator.network' is undefined, then either phonegap.js is not
loaded completely yet or is not being referenced in your phonegap page
at all. Since the XHR implementation is done natively in Java on the
blackberry, we need to bridge into native using the PhoneGap approach,
so you need to have phonegap.js loaded in.

Very simple example:

<html>
<head>
<!-- make sure phonegap.js is in the same directory as this html page
-->
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript">
// Renders the JSON data returned by the twitter api into the DOM
function twitterCallback(obj) {
var payload = obj.results;
var html = '';
for (var tweet in payload) {
html += tweet.text + ' || ';
}
document.getElementById('content').innerHTML = html;
}
// Runs the XHR command.
function runXHR() {
navigator.network.XHR("http://search.twitter.com/search.json?
q=phonegap", null, twitterCallback);
}
// Makes sure the page is loaded before initiating the XHR.
function loadIt() {
var state = document.readyState;
if (state == "loaded" || state == "complete") {
runXHR();
} else {
setTimeout(runXHR, 250);
}
}
</script>
</head>
<body onload="loadIt();">
<p id="content"></p>
</body>
</html>

To clarify: the 2nd parameter to the XHR() function is the POST data
to write to the body of the request, so should be ONE OF:
a) null. If you are making a GET request, pass in the null literal
(since you are not writing any POST data).
b) a string. this represents the POST data you are attaching to the
request.

Hope that helps.

Udham Singh

unread,
Apr 20, 2010, 2:02:21 AM4/20/10
to phonegap
Thank you filmaj,

I did what you given above, but the callback method
not fired. My remote file can be accessed from Android and other
browser. I am using your lates phonegap-blackberry project.


Thanks

filmaj

unread,
Apr 20, 2010, 1:34:47 PM4/20/10
to phonegap
Are you running from simulator or from device?

If from simulator, make sure you have the MDS-CS program running in
the background - this is the internet proxy that allows the simulator
to communicate with the interwebz. You can manually run it from your
eclipse directory if you are using the BlackBerry Eclipse plugin, it
should be located in eclipse/plugins/
net.rim.eide.componentpack<version>/components/MDS/run.bat. Make sure
you run it from the proper directory inside the plugins/ directory,
that is, for example, if you are running the 4.6.1 simulator, make
sure you run the run.bat file located in
net.rim.eide.componentpack4.6.1/components/MDS/

If you are running on device, make sure your APN settings are set
correctly, Menu -> Options -> Advanced Options -> TCP/IP

Udham Singh

unread,
Apr 21, 2010, 8:07:10 AM4/21/10
to phonegap
Thank you very much filmaj. It is working now.

karthik sasidharan

unread,
Apr 21, 2010, 5:33:19 AM4/21/10
to filmaj, phonegap
Hi

Even i am facing the same problem, i am running from the simulator, running the MDS-CS program also did not help

thanks
Karthik.S

filmaj

unread,
Apr 21, 2010, 8:26:35 PM4/21/10
to phonegap
Hey Karthik,

There may be a disconnect between the headers of the request being
sent out and the headers of your server's response. Have a look at
this link:

http://github.com/phonegap/phonegap-blackberry/blob/master/framework/src/com/nitobi/phonegap/api/impl/NetworkCommand.java#L225

This is the code that runs the custom PhoneGap XHR implementation. At
line 225, I would suggest you place a breakpoint and run your app in
the simulator in debug mode. At this line, we check the response code.
If it is anything other than 200, no JavaScript call will be made back
in the PhoneGap browser. In my experience, this happens sometimes if
the ACCEPT- headers in your request don't match up to the same headers
that your server accepts. The dozen or so lines above line 225 are
well commented with various options on what to set the request headers
to.

Usually I just check the HTTP response code, and that gives me a good
enough clue as to what the problem is, if the response code is
something other than 200.

Hope that helps!
Let me know how it goes.

Cheers,
Fil Maj
Web Software Engineer
Nitobi

On Apr 21, 2:33 am, karthik sasidharan <karthiksasidhar...@gmail.com>
wrote:
> > > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > > > > For more options, visit this group at
> > > > > > >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > > > Groups "phonegap" group.
> > > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > > To unsubscribe from this group, send email to
> > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "phonegap" group.
> > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > To unsubscribe from this group, send email to
> > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "phonegap" group.
> > > > To post to this group, send email to phon...@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "phonegap" group.
> > > To post to this group, send email to phon...@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > For more options, visit this group at
> >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go towww.phonegap.com

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com

azayed

unread,
Apr 22, 2010, 9:17:56 AM4/22/10
to phonegap

filmaj , or any body, Pleae can you give an example of how did you
write your code to let it work for xml document post ? (sorry of
asking too much)

Following you example I did the following but it didn´t work :

function xmlCallback(obj) {

var payload = obj.results;
var html = "";
for (var code in payload) {
alert("lanzado");
html += codigo.text + " || ";

}
document.getElementById('content').innerHTML = html;
}

// Runs the XHR command.
function runXHR() {

var postData="<record><login><usuario>usuer</
usuario><contrasena>password</contrasena></login></record>"
navigator.network.XHR("the url of the service", postData,
xmlCallback);
}

// Makes sure the page is loaded before initiating the XHR.
function loadIt() {
var state = document.readyState;
if (state == "loaded" || state == "complete") {
runXHR();
} else {
setTimeout(runXHR, 250);
}
}

On 22 abr, 02:26, filmaj <filip....@nitobi.com> wrote:
> Hey Karthik,
>
> There may be a disconnect between the headers of the request being
> sent out and the headers of your server's response. Have a look at
> this link:
>
> http://github.com/phonegap/phonegap-blackberry/blob/master/framework/...
> > > > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > > > > For more options, visit this group at
> > > > > > > >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > > > For more info on PhoneGap or to download the code go
> > > towww.phonegap.com
>
> > > > > > > --
> > > > > > > You received this message because you are subscribed to the Google
> > > > > > > Groups "phonegap" group.
> > > > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > > > To unsubscribe from this group, send email to
> > > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > > > For more options, visit this group athttp://
> > > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > > For more info on PhoneGap or to download the code go
> > > towww.phonegap.com
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > > > Groups "phonegap" group.
> > > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > > To unsubscribe from this group, send email to
> > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > > For more options, visit this group athttp://
> > > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > For more info on PhoneGap or to download the code go
> > > towww.phonegap.com
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "phonegap" group.
> > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > To unsubscribe from this group, send email to
> > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > For more options, visit this group athttp://
>
> ...
>
> leer más »

filmaj

unread,
Apr 23, 2010, 7:05:37 PM4/23/10
to phonegap
azayed,

the current implementation is very rough, it is just a temporary
solution for my own purposes that we basically opened up for other
people to use. Thus, the current implementation is made to work with
JSON responses (something that the JS interpreter can easily
evaluate). For XML, I have no solution...

The main problem with trying to parse XML within the BlackBerry
browser is that there is only DOM level 2 which only supports certain
types of DOM objects. The problem is that the RIM browser doesn't have
a native DOMParser object (which is DOM level 3), so parsing XML is
difficult unless you build or find some kind of javascript library
that will do text-based parsing (or some such).

For my own previous client projects, what I did was added a
conditional statement to the NetworkCommand.java NetworkThread
implementation, that checked the server response MIME type after the
request is dispatched - if it was text/xml, then I would do the XML
parsing on the *Java* side and then simply marshall a JSON
representation that I would build up back into the browser after the
parsing was complete. Definitely sucks, but actually as long as you
import some of the "sax" XML parsing libraries that are available to
you via the RIM API the code is quite manageable and simple to write.
Hopefully you're cool with Java programming!

Man I can't wait for the new RIM webkit browser... I hope to see it
out there within 6 months!
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages