Nusoap webservice不与Xamarin Android应用程序通信

时间:2016-02-16 17:30:36

标签: android web-services visual-studio-2013 xamarin nusoap

我使用nusoap开发了一个web服务,看来web服务工作正常,其实很简单,我在这里放了代码:

<?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 :(");
            }
        };

    }

然后,当我单击按钮并且应用程序应该执行插入时,我遇到以下异常: enter image description here

我认为它正在发生,因为SOAP对象的格式不正确,但我无法查看错误在哪里:(

我真的很感激任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

当客户端无法与服务器建立连接时,抛出此异常。在这种情况下(我使用Hostinger)我更改了mysql.hostinger.com的数字服务器IP,翻译的域名实际上是相同的...但是由于我不知道的原因它只是使用翻译的域名。 我的代码:

 <?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('AWebService', 'urn:AWebService');
// Character encoding
$server->soap_defencoding = 'utf-8';

//Register Insert user function
$server->register(
                  'InsertData',   //Name of function
                  array('userName' => 'xsd:string',
                        'Pass' => 'xsd:string',
                        'Mail' => 'xsd:string'), //Insert Values
                  array('return' =>'xsd:boolean'), //return Values
                  'urn:AwebServiceWsdl',  //Namespace
                  'urn:AwebServiceWsdl#InsertData',  //SoapAction
                  'rpc',       //style
                  'literal',   //can be encoded but it doesn't work with silverlight
                  'Insert function to register users'
                  );

   function InsertData($userName, $Pass, $Mail) {
    $connect = mysqli_connect("mysql.hostinger.es", [USER], [PASSWORD], [DATABASE]);
    if ($connect) {

        mysqli_query($connect, "INSERT INTO `Users`(`UserName`, `Pass`, `Mail`) VALUES ('".$userName."','".$Pass."','".$Mail."')");
                     return true;
                     }
                     return false;
                     }


$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);


?>

顺便说一句,我将mysql_connect()方法更改为mysqli_connect()方法,因为第一个方法已被弃用。另一个mysql方法也改变了。 更多信息:

Deprecated: mysql_connect()