kwilliamson
kwilliamson

Reputation: 61

PhoneGap + Android + jQuery Mobile - Connection to server unsuccessful

I'm trying to create an Android (2.2) app using Phonegap (version 1.9.0) and jQuery Mobile (1.1.0). Specifically, the app is supposed to send a GET request to an existing WCF REST service and retrieve JSON data (a list of folders) and display each item as an option in a select drop down menu.

Right now, all this works as it should when I use the desktop browser. The WCF REST service sends the correct response, the browser renders the page correctly and puts the options in the select menu as I want it to. However, when I use the following code in Android app using Phonegap:

$.getJSON('http://xxx.xxx.xxx/MobileService.svc/GetFolders?callback=?', null, function (folders) {                  
    $.each( folders, function( i, folder ) {
        $("#folders").append("<option value='"+folder.Id+"'>"+folder.Name+"</option>");
    });
    $("#folders").selectmenu("refresh");
});

...I get the error message when I boot the Android app (debugging on physical device):

Connection to the server was unsuccessful. ("file:///android_asset/www/index.html")

When I comment out the above $.getJSON code, the app loads fine, but the select menu is unpopulated.

I've also noticed that apparently because Phonegap uses the "file://" protocol, it is not affected by the "same-origin policy" that the "http://" protocol is...I had been using JSONP when I was working with the browser, which is why I have the callback. I don't think this should be the reason why it is failing, but I dont know. Any help will be greatly appreciated!

Things I've tried:

Upvotes: 1

Views: 3837

Answers (2)

kwilliamson
kwilliamson

Reputation: 61

It turns out in my particular case that the issue boiled down to the mobile phone being on guest wifi network that didn't have access to the network where the REST service was hosted so the connection was being blocked.

Hope that can help someone

Upvotes: 1

Calavoow
Calavoow

Reputation: 492

You can try to set your cordova.xml to "*" instead of ".*" as seen on the Getting Started Guide

Or even better:

 http://xxx.xxx.xxx

Upvotes: 1

Related Questions