android - Yii 和 Android 之间的 SOAP

标签 android web-services soap yii

美好的一天。 我尝试用 yii 和 android 客户端创建 web 服务服务器。 这是我的 WSDL,我在本地主机上创建了它,URL 是 http://localhost:8888/places/index.php?r=Service/service

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="urn:ServiceControllerwsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" name="ServiceController" targetNamespace="urn:ServiceControllerwsdl">
<wsdl:message name="registrateRequest">
<wsdl:part name="login" type="xsd:string"/>
<wsdl:part name="password" type="xsd:string"/>
<wsdl:part name="email" type="xsd:string"/>
<wsdl:part name="name" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="registrateResponse">
<wsdl:part name="return" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="authenticateRequest">
<wsdl:part name="login" type="xsd:string"/>
<wsdl:part name="password" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="authenticateResponse"/>
<wsdl:portType name="ServiceControllerPortType">
<wsdl:operation name="registrate">
<wsdl:documentation/>
<wsdl:input message="tns:registrateRequest"/>
<wsdl:output message="tns:registrateResponse"/>
</wsdl:operation>
<wsdl:operation name="authenticate">
<wsdl:documentation/>
<wsdl:input message="tns:authenticateRequest"/>
<wsdl:output message="tns:authenticateResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServiceControllerBinding" type="tns:ServiceControllerPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="registrate">
<soap:operation soapAction="urn:ServiceControllerwsdl#registrate" style="rpc"/>
<wsdl:input>
<soap:body use="encoded" namespace="urn:ServiceControllerwsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="urn:ServiceControllerwsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="authenticate">
<soap:operation soapAction="urn:ServiceControllerwsdl#authenticate" style="rpc"/>
<wsdl:input>
<soap:body use="encoded" namespace="urn:ServiceControllerwsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="urn:ServiceControllerwsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ServiceControllerService">
<wsdl:port name="ServiceControllerPort" binding="tns:ServiceControllerBinding">
<soap:address location="http://localhost:8888/places/index.php?r=service/service&ws=1"/>
</wsdl:port>
</wsdl:service>
</definitions>

我在 yii 中用这个类创建它

<?php

class ServiceController extends CController
{
    public function actions()
    {
        return array(
            'service'=>array(
                'class'=>'CWebServiceAction',
            ),
        );
    }
    /**
     * @param string login
     * @param string password
     * @param string email
     * @param string name
     * @return string 
     * @soap
     */
    public function registrate($login, $password, $email, $name)
    {
        $user = new UserModel();
        /*create new user*/
        $user->login = $login;
        $user->password = md5($password);
        $user->email = $email;
        $user->name = $name;
        $user->active = 0;
        //save to DB
        $user->save(); 
        return 'success';

    }
    /**
     * @param string login
     * @param string password
     * return int
     * @soap
     */
    public function authenticate($login, $password)
    {
        $post=Post::model()->find('postID=:postID', array(':postID'=>10));
        $user = UserModel::model()->find('login:=login and password:=password', 
                                    array(':login' => $login, ':password'=>$password));
        if (!empty ($user)){
            return 1;
        }
        else{
            return 0;
        }

    }
}

当我尝试在 android whith ksoap 上连接到它时

public class PlacesActivity extends Activity {
    /** Called when the activity is first created. */
    private static String SOAP_ACTION = "urn:#registrate";
    private static String NAMESPACE = "urn:ServiceControllerwsdl";
    private static String METHOD_NAME = "registrate";
    private static String URL = "http://localhost:8888/places/index.php?r=Service/service";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       
            //Use this to add parameters
            request.addProperty("login","nabiullin11");
            request.addProperty("password","qwer1234");
            request.addProperty("email","nabiullinas@gmail.com");
            request.addProperty("name","nabiullin11");
            //Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            //Needed to make the internet call
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);
            } catch (Exception e) {
                TextView t = (TextView)this.findViewById(R.id.resultbox);
                //Get the first property and change the label text
                t.setText("FAIL");
                e.printStackTrace();
            }
            SoapObject result = (SoapObject)envelope.bodyIn;
            if(result != null){
                TextView t = (TextView)this.findViewById(R.id.resultbox);
                //Get the first property and change the label text
                t.setText("SOAP response:\n\n" + result.getProperty(0).toString());
            }

    }

}

我总是失败。我的wsdl有问题吗?或者我有这样的问题,因为尝试连接到本地主机服务器?

最佳答案

尝试使用此参数,我在使用 yii 和 android 时遇到了同样的问题,这是解决方案:

static String SOAP_ACTION = "urn:ServiceControllerwsdl#authenticate";
private static String NAMESPACE = "urn:ServiceControllerwsdl";
private static String METHOD_NAME = "authenticate";
private static String URL = "http://localhost:8888/places/index.php?r=service/service&ws=1";

关于android - Yii 和 Android 之间的 SOAP ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018077/

相关文章:

android - 奇怪的 MapView 问题;像素转换器错误

Android 动态壁纸在配置壁纸时崩溃

java - 使用layout_width ="wrap_content"获取TextView的可用宽度

android - 如何从 proguard 中排除 .so 文件?

c# - 是否可以创建具有未定义类型的方法?

java - 不允许混合使用 XOP/MTOM 和附件

web-services - Java 网络服务不适用于 Jdk 6 和 Jboss 5.x

python - 如何添加wsu :Id attribute using zeep?

node.js - 使用 X509 证书实现 WSSecurity npm 'Soap'

java - 当没有定义请求对象时,如何使用 Spring Boot 发送 SOAP 请求