从Android调用WebService时出错

时间:2014-09-17 17:25:21

标签: android web-services ksoap2 android-ksoap2

我正在尝试在我的Android应用中调用http://www.w3schools.com/webservices/tempconvert.asmx的网络服务CelsiusToFahrenheit。我正在使用kso​​ap2,我将参数设置如下:

private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
private static final String URL = "http://www.w3schools.com/webservices/";

按如下方式调用服务:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("CelsiusToFahrenheit", 32);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalDouble marshaldDouble = new MarshalDouble();
marshaldDouble.register(envelope);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope); // Throws Error

Object result = (Object)envelope.getResponse();
String[] results = (String[])  result;

但是行androidHttpTransport.call(SOAP_ACTION,envelope);抛出一个错误,说无法解析主机" www.w3schools.com":没有与主机名相关的地址。

任何帮助都将是值得赞赏的。我的猜测是我没有正确设置SOAPAction,URL或Method_NAME,Namespace。如果你需要看一下,这是http://www.w3schools.com/webservices/tempconvert.asmx?WSDL

最终修改:我可以按照以下Android - What should I use to get data from remote db?

中链接中的说明解决此问题

1 个答案:

答案 0 :(得分:0)

我查看了您的SOAP_ACTION链接,看起来它无效。

http://www.w3schools.com/webservices/CelsiusToFahrenheit”返回“404 - 无法找到页面”

你需要做的就是改变它。

你的POST看起来应该更像这样:

POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.w3schools.com/webservices/tempconvert.asmx"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
      <Celsius>string</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>