【发布时间】:2012-08-15 01:42:47
【问题描述】:
到目前为止,我已经创建了一个 PHP 客户端和一个 VB.NET 客户端,它们都成功地调用了我的 PHP Web 服务。为了让后者工作,我需要使用来自SourceForge 的 SoapUI 工具。它告诉我我的 wsdl 不符合 WS-I。我不需要 Pro 版本以交互方式测试我的服务,因为它允许您直接编辑肥皂请求。在修复了我的 WSDL 并让我的 VB.Net 客户端运行 android 之后仍然是一个问题。
我也attached the source code for ksoap2-andriod 以便我可以在调试时逐步完成。它有一点帮助,但有一些捆绑的依赖项不包括源代码,特别是“kxml2 v1.6”。如果有人能指出我的源 zip 或 Jar,我将不胜感激。
这是我从我的 android 客户端调用 PHP 网络服务时无法克服的错误。
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <definitions name='naturallyIrrationalsoapserver' targetNamespace='http://www.naturallyIrrational.com'>@10:42 in java.io.InputStreamReader@44dce560)
它告诉我它无法解析 WSDL\XML - 位置 @10.42 是开始定义标记的结尾。
我相信,由于现在 WS-I 中的 WSDL 符合 Ksoap2 的解释,因此问题出在服务命名空间定义上。这是我用来调用它的android客户端代码。
public class SoapClientActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.myText);
String soapResponse="";
final String METHOD_NAME = "getArrval";
final String SOAP_ACTION = "http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.wsdl";
final String NAMESPACE = "http://www.naturallyIrrational.com";
* 下一行不正确,应该指向 http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.php *
final String URL = "http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.wsdl";
if (InternetStatus.getInstance(this).isOnline(this)) {
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("valname");
pi.setValue("rt2");
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=false;
envelope.setOutputSoapObject(request);
HttpTransportSE andHttpTransport = new HttpTransportSE(URL);
andHttpTransport.debug = true;
andHttpTransport.call(SOAP_ACTION, envelope);
*返回这个值有问题,已经收到但是从ksoap传回时抛出错误*
soapResponse = (String) envelope.getResponse(); //Exception is thrown here
String strrequest = andHttpTransport.requestDump;
String strresponse = andHttpTransport.responseDump;
}catch(Exception e){soapResponse = "Nope not working "+"\n\n" + e.getMessage() + "/n/n" + e.getStackTrace() ;}
} else {soapResponse="You are not online"; }
tv.setText(soapResponse);
}
}
如果我做了一些愚蠢的事情并且有人能指出我会很感激。
有没有像 PHP 那样在 Android 中不缓存和重用 wsdl 的指令?
也许使用 net beans 会有所帮助,那是我有时间的下一个。如果有人可以在此期间提供帮助,请随时提出建议。
<?php
class naturallyIrrational {
private $arrval = array("pi" => 3.1415,"e" => 2.7183, "rt2" => 1.414, "phi" => 1.618 );
function getIrrationalvalue($valname) {
$myFile = "logFile.html";
$fh = fopen($myFile, 'a') or die("can't open file");
$datq = date('m/d/Y h:i:s a', time());
fwrite($fh, $datq);
if (isset($this->arrval[$valname])) {
$stringData = " ".$valname." = ".$this->arrval[$valname]."<br/>";
fwrite($fh, $stringData);
fclose($fh);
return $this->arrval[$valname];
} else {
throw new SoapFault("Server","Unknown Symbol '$valname'.");
}
}
}
$server = new SoapServer("naturallyIrrational.wsdl");
$server->setClass("naturallyIrrational");
$server->handle();
【问题讨论】:
-
已指定 WSDL:w3.org/TR/wsdl
-
在编写/使用 Web 服务时,我目前的看法是“除了 SOAP”。 SOAP 代表“严重过度架构的协议”。设置起来很痛苦,很慢,而且很脆弱。有许多更好的替代方案。如果您有选择,转储 SOAP,然后使用 Rest、JSON 或 Thrift。
-
感谢您的评论,我还将研究一个 Restful JSON 解决方案。尽管我的问题是关于让 SOAP 工作的,但尊重我。我想确信我创建的 WSDL 是正确的。我将研究 WSDL 规范(感谢 hakra 链接),除非有捷径可以帮助我......就像 .NET soap 工具包使用实用程序自动创建 WSDL。
-
SOAP 和 Restful 相比其他有优势。但大多数情况下使用的是 restful,因为它是轻量级的,并且支持所有 MIME 媒体类型,例如 JSON、XML、tex、urlencored 等等。
-
再次感谢,但这仍然是一个 WSDL/SOAP 问题。
标签: php android soap android-ksoap2