问题
I'm running the latest version of android and it's SDK and I'm using phonegap to create a mobile application. My problem is that I am requesting a web service and I am displaying the contents of the received message in a tag in my html. The web service is working fine, the displaying feature is working fine, and it's even running on the emulator without any bugs. The problem arises when I try to run the same application and code on a real android device.... nothing happens. The devices I am testing on have android version 2.3.1 and 4.0. The emulator that Im using to test on is 2.2 and the android project was created with android sdk version 2.2. To make sure the issue was unrelated to android OS version, I also created a test project using version 4.0 and tested it on the android device having the same OS version and I get the same problem.
Here is a snippet of my code.
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://feeds.delicious.com/v2/json/popular", false);
xhr.send(null);
if (xhr.readyState==4 && xhr.status==200){
clearLoadingScreen();
var popular= $("#disclaimer").html("");
popular= "";
$("#popular").append('<div class="popular"><p>Popular:</p>'+ "This is an example popular list<br />" + popular+ ' </div>');
}
like I said, the code works perfectly on the emulator, when the index.html page loads, I see a blank screen and a few seconds later the is populated. When I port the code to the phone, it does not work. Does anyone have any ideas why?
回答1:
Check for xhr.status==0 as well. Sometimes when running from the file:// protocol you will get a status of 0.
回答2:
Check your policy located in: project/res/xml/cordova.xml (if using Cordova - which is phonegap 1.6? or higher)
It's called phonegap.xml if on a lower version.
来源:https://stackoverflow.com/questions/10523720/phonegap-android-project-works-on-emulator-not-on-actual-phone