【问题标题】:Phonegap + Android status=0 returned from webservicePhonegap + Android 状态=0 从 web 服务返回
【发布时间】:2012-06-27 16:14:45
【问题描述】:

Phonegap 团队,

我在使用 android 访问网络服务时遇到问题。我使用iOS访问它没有问题。 随附的代码使用公共网络服务,因此如果您愿意,可以尝试该代码。

在 iOS 上,我们得到一个 xmlhttp.status == 200 并返回数据。 在 Android 上,我们得到一个 xmlhttp.status == 0。

我们正在使用cordova-1.8.1.jar

我们在 res/xml/cordova.xml 中设置了白名单 像这样:

<access origin=".*"/>

我提出这个问题是因为我怀疑我们的白名单不起作用。

代码如下:

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 );

}

【问题讨论】:

标签: android web-services cordova


【解决方案1】:

有时,当您从文件协议执行 AJAX 请求时,您将获得以下状态 0,但实际上是 200。如果是,请更改您:

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

你应该很高兴。

这是我写的一篇关于使用 PhoneGap 的 AJAX 的博文。

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

更新:显然在 Android 2.x 中存在一个错误,其中设置“Content-length”标头会导致您描述的问题。看起来该错误已在 Android 4.0.3 中修复。因此,在 4.0.3 模拟器中尝试未修改的这段代码,它应该可以工作,然后回到 2.x 并删除 Content-length 标头,看看它是否也能正常工作。

【讨论】:

  • 根据 Simon 的建议,我添加了这个,我得到的长度为 0: if(xhr.status == 200 || xhr.status == 0){ alert("responseText.length ="+xhr .responseText.length);
  • 根据 Simon 对他的建议的更新,我们在 4.0.3 上进行了尝试并得到了相同的结果。我们提供的示例代码使用公共网络服务,因此其他人可以运行它,看看我是不是疯了。
  • 我们还删除了 content-length。
【解决方案2】:

我们放弃了尝试在 Javascript 中运行 web 服务,并编写了一个 Java 插件来运行 web 服务。我们需要它在各种各样的设备上工作,而不是依赖用户更新他们的设备。

【讨论】:

    【解决方案3】:

    尝试在 src > com.domain.appname > appname.java 之后添加以下代码 super.onCreate(savedInstanceState);

    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2019-01-02
      • 2011-10-28
      • 1970-01-01
      • 2021-12-18
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多