【发布时间】:2016-02-18 05:21:52
【问题描述】:
我使用nusoap开发了一个web service,看起来web service运行良好,其实很简单,我把代码放在这里:
<?php
// Pull in the NuSOAP
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
//(MyService is name of our service)
$server->configureWSDL('WebService', 'urn:WebService');
// Character encoding
$server->soap_defencoding = 'utf-8';
//Register Insert user function
$server->register(
'InsertData', //Name of function
array('Id' => 'xsd:int',
'userName' => 'xsd:string',
'Pass' => 'xsd:string',
'Mail' => 'xsd:string'), //Insert Values
array('return' =>'xsd:boolean'), //return Values
'urn:ServiceWsdl', //Namespace
'urn:ServiceWsdl#InsertData', //SoapAction
'rpc', //style
'literal', //can be encoded but it doesn't work with silverlight
'Insert function to register users'
);
//Register GetData function
$server->register(
'GetData',
array('Id' => 'xsd:int'),
array('Id' => 'xsd:int',
'userName' => 'xsd:string',
'Pass' => 'xsd:string',
'Mail' => 'xsd:string'), //return values
'urn:ServiceWsdl',
'urn:ServiceWsdl#GetData',
'rpc',
'literal',
'Get all users function'
);
function InsertData($id, $userName, $Pass, $Mail) {
$connect = mysql_connect("server","userDB","passDB");
if ($connect) {
if(mysql_select_db("database", $connect)) {
mysql_query( "INSERT INTO `Users`(`Id`, `UserName`, `Pass`, `Mail`) VALUES (`".$id."`,`".$userName."`,`".$Pass."´,`".$Mail."`);");
return true;
}
}
return false;
}
function GetData($Id) {
$connect = mysql_connect("server","userDB","passDB");
if ($connect) {
if(mysql_select_db("database", $connect)) {
$sql = "SELECT * FROM Users";
$result = mysql_fetch_array(mysql_query($sql));
return $result['Id']."-".$result['UserName']."-".$result['Pass']."-".$result['Mail'];
}
}
return false;
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
我在 Android Xamarin 项目中的代码(使用 Visual Studio 2013)也非常简单:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = (Button) FindViewById<Button>(Resource.Id.MyButton);
TextView labelText = (TextView)FindViewById<TextView>(Resource.Id.editTextUserName);
button.Click += delegate
{
//showMessageWithName(labelText.Text);
AvitecWS service = new AvitecWS();
if (service.InsertData(69, "AnUser", "anUserPassword", "anUser@mail.com"))
{
//the following method just show a message :)
showMessageWithName("Message has been send!");
}
else
{
showMessageWithName("Upss... something was wrong :(");
}
};
}
然后,当我单击按钮并且应用程序应该进行插入时,我遇到了以下异常:
我认为这是因为 SOAP 对象的格式不正确,但我无法查看错误在哪里:(
非常感谢任何帮助。
提前致谢。
【问题讨论】:
-
您使用 SOAP 有什么特别的原因吗?处理起来很痛苦,我会推荐一个使用 JSON 的简单 REST 服务。
-
嗯....例如?你能给我推荐一个图书馆吗?
-
好吧...那是真的...我看到了光!烧死我了!!! ;) 但是......只是为了好奇,我得到了很多关于 nusoap 的文档,嗯......似乎我的代码没问题 :( 顺便说一句,有人知道关于 REST 使用 PHP 的好教程吗?
-
Google "php rest framework" 返回几个有趣的结果。
-
不错的贡献... ;) 最后我实现了它工作正常。我发布我的代码作为答案,以便于阅读增加 stackoverflow 知识数据库。 ;)
标签: android web-services visual-studio-2013 xamarin nusoap