3

Phonegap Crew,

I have an problem with accessing a webservice using android. I have no problem accessing it using iOS. The enclosed code uses a public webservice so you can try the code if you are so inclined.

On iOS we get a xmlhttp.status == 200 and returned data. On Android we get a xmlhttp.status == 0.

We are using cordova-1.8.1.jar

We have the white list set in res/xml/cordova.xml like this:

<access origin=".*"/>

I am bring that up because I am suspicious that our white list is not working.

here is the code:

function testweather(){
   var xhr= new XMLHttpRequest();
   xhr.onreadystatechange = function(){

      alert(xhr.readyState);
      if(xhr.readyState == 4){
         if(xhr.status == 200){
            $( "#result" ).append( xhr.responseText );
         }
         else{
            alert("can't get response. a.status:"+xhr.status);
         }
      }
   }

var url = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php";
xhr.open("POST", url,true);
xhr.setRequestHeader("SOAPAction",   "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDayLatLonList");
xhr.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
xhr.setRequestHeader("Content-Length", 1536);
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
xhr.setRequestHeader("User-Agent", "IBM Web Services Explorer");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Pragma", "no-cache");
xhr.setRequestHeader("Connection", "close");
var soapEnv = '' +
    '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">' +
    '   <soapenv:Header/>' +
    '   <soapenv:Body>' +
    '      <ndf:NDFDgenByDayLatLonList soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
    '         <listLatLon xsi:type="dwml:listLatLonType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">35.4,-97.6</listLatLon>' +
    '         <startDate xsi:type="xsd:date">2012-06-27</startDate>' +
    '         <numDays xsi:type="xsd:integer">3</numDays>' +
    '         <Unit xsi:type="dwml:unitType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">e</Unit>' +
    '         <format xsi:type="dwml:formatType" xmlns:dwml="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">24 hourly</format>' +
    '      </ndf:NDFDgenByDayLatLonList>' +
    '   </soapenv:Body>' +
    '</soapenv:Envelope>';

xhr.send( soapEnv );

}
Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
  • Here is the right answer [go here][1] [1]: http://stackoverflow.com/questions/14495403/soap-request-javascript-in-browser-not-giving-response – user2007354 Jan 24 '13 at 12:45
  • If this problem is already solved, I would be glad to hear the solution... – Alexander Feb 17 '15 at 12:47

3 Answers3

5

Sometimes when you do an AJAX request from the file protocol you will get a status of 0 but that is effectively a 200. Just change you if to be:

if(xhr.status == 200 || xhr.status == 0)

and you should be good to go.

Here is a blog post I wrote on using AJAX from PhoneGap.

http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

Updated: Apparently there is a bug in Android 2.x where setting the "Content-length" header causes the problem you describe. It looks like the bug has been fixed in Android 4.0.3. So try this code unmodified in the 4.0.3 emulator and it should work then come back to 2.x and remove the Content-length header to see if it works as well.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
0

We gave up trying to run the webservice in Javascript and wrote a Java plugin to run the webservice. We needed it to work on a wide variety of devices and not rely on the user updating their device.

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
-1

Try adding the below code in src > com.domain.appname > appname.java after super.onCreate(savedInstanceState);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Manu Janardhanan
  • 600
  • 4
  • 10