1

我正在尝试调用 PHP SOAP 网络服务。我知道我的 web 服务功能正确,因为我在 WPF 项目中成功使用了它。我还在使用相同的 web 服务在 android 中构建一个应用程序。

可以在此处找到 WSDL 文件:http ://www.wegotcha.nl/servicehandler/service.wsdl

这是我在 android 应用程序中的代码:

String SOAP_ACTION = "http://www.wegotcha.nl/servicehandler/CheckLogin";
        String NAMESPACE = "http://www.wegotcha.nl/servicehandler";
        String METHOD_NAME = "CheckLogin";
        String URL = "http://www.wegotcha.nl/servicehandler/servicehandler.php";
        String resultData = "";

        SoapSerializationEnvelope soapEnv = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapObject UserCredentials = new SoapObject("Types", "UserCredentials6");
        UserCredentials.addProperty("mail", params[0]);
        UserCredentials.addProperty("password", md5(params[1]));

        request.addSoapObject(UserCredentials);
        soapEnv.setOutputSoapObject(request);

        HttpTransportSE http = new HttpTransportSE(URL);
        http.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        http.debug = true;

        try {
            http.call(SOAP_ACTION, soapEnv);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        SoapObject results = null;
        results = (SoapObject)soapEnv.bodyOut;

        if(results != null)
            resultData = results.getProperty(0).toString();

        return resultData;

使用提琴手我得到以下信息: Android 请求:

<?xml version="1.0" encoding="utf-8"?>
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header />
<v:Body>
<n0:CheckLogin id="o0" c:root="1" xmlns:n0="http://www.wegotcha.nl/servicehandler">
<n1:UserCredentials6 i:type="n1:UserCredentials6" xmlns:n1="Types">
    <mail i:type="d:string">myemail</mail>
    <password i:type="d:string">myhashedpass</password>
</n1:UserCredentials6>
</n0:CheckLogin>
</v:Body>
</v:Envelope>

得到以下响应:

 Procedure 'CheckLogin' not present

我的 WPF 应用程序生成的请求看起来完全不同:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserCredentials6 xmlns="Types">
    <mail>mymail</mail>
    <password>mypassword</password>
</UserCredentials6>
</s:Body>
</s:Envelope>

在谷歌搜索我的屁股后,我无法自己解决这个问题。我的 Java 代码中可能有一些奇怪的东西,因为从那以后我改变了很多。我希望你们能帮助我,谢谢。

编辑:

我的网络服务是文档/文字编码风格,在做了一些研究后我发现我应该能够使用SoepEnvelope而不是SoapSerializationEnvelope虽然当我替换它时,我在尝试缓存块之前得到一个错误,导致我的应用程序崩溃。

错误:

11-04 16:23:26.786: E/AndroidRuntime(26447): 由: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject 无法转换为 org.kxml2.kdom.Node

这是由这些行引起的:

request.addSoapObject(UserCredentials);
soapEnv.setOutputSoapObject(request);

不过,这可能是一个解决方案,我该怎么做呢?除了这个很棒的教程之外,我没有发现关于使用 SoapEnvelope 而不是 SoapSerializationEnvelope 的任何信息:

http://ksoap.objectweb.org/project/mailingLists/ksoap/msg00849.html

4

1 回答 1

1

刚刚发生了一些奇怪的事情。我只是删除了 a 行(我做了数千次)调整了其余部分,因为我遇到了一些新错误并且我得到了以下代码:

String SOAP_ACTION = "http://www.wegotcha.nl/servicehandler/CheckLogin";
String NAMESPACE = "http://www.wegotcha.nl/servicehandler";
String METHOD_NAME = "CheckLogin";
String URL = "http://www.wegotcha.nl/servicehandler/servicehandler.php";
String resultData = "";

SoapSerializationEnvelope soapEnv = new SoapSerializationEnvelope(SoapEnvelope.VER11);

SoapObject UserCredentials = new SoapObject("Types", "UserCredentials6");
UserCredentials.addProperty("mail", params[0]);
UserCredentials.addProperty("password", md5(params[1]));

// changed these lines
// request.addSoapObject(UserCredentials);
// soapEnv.setOutputSoapObject(request);
// into:
soapEnv.setOutputSoapObject(UserCredentials);

HttpTransportSE http = new HttpTransportSE(URL);
http.debug = true;

// little tweak here        
String results = null;
try {
    http.call(SOAP_ACTION, soapEnv);
    //another tweak here
results = soapEnv.bodyIn.toString();
} catch (IOException e) {
    e.printStackTrace();
} catch (XmlPullParserException e) {
    e.printStackTrace();
}

if(results != null)
    resultData = results;

return resultData;

现在我可以正确登录了 :) 它们不是非常实用的解决方法,因为这是唯一可以让我回复“是”或“否”的地方。想想复杂类型的数组等。但那是另一回事了。

于 2012-11-04T16:49:48.983 回答