简体   繁体   English

来自ksoap2-android的ClassCastException响应

[英]ClassCastException from ksoap2-android response

I am a beginner in Java and Android and have struggled for the better part of the day with the following problem: 我是Java和Android的初学者,并且在一天的大部分时间里都遇到以下问题:

I use ksoap2 to call a login method from a webservice. 我使用kso​​ap2从Web服务调用登录方法。 I expect to get a User object as response, not a SoapObject. 我希望得到一个User对象作为响应,而不是SoapObject。

The call from Android is: 来自Android的电话是:

   private static final String URL = "http://192.168.0.13:8080/LoginWS/LoginWS?wsdl";
    private static final String NAMESPACE = "http://ws/";
    private static final String METHOD_LOGIN = "login";

    ......
    SoapObject request = new SoapObject(NAMESPACE, METHOD_LOGIN);
            SoapSerializationEnvelope env =
                    new SoapSerializationEnvelope(SoapEnvelope.VER11);


            request.addProperty("user", user);


            env.setOutputSoapObject(request);
            env.addMapping(NAMESPACE, "user", User.class);

            HttpTransportSE transport = new HttpTransportSE(URL);
            transport.debug = true;
            transport.call(NAMESPACE + METHOD_LOGIN, env);

            SoapObject response = (SoapObject) env.getResponse();
            System.out.println(response);

    ......

The Android User class is: Android User类为:

public class User implements KvmSerializable{


    private int id;
    private String username;
    private String password;
    private String telephone;

    public User(){
    }

    public User(String username, String password, String telephone){
        this.username = username;
        this.password = password;
        this.telephone = telephone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return telephone;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.telephone = phoneNumber;
    }

    @Override
    public Object getProperty(int i) {
        switch(i)
        {
            case 0:
                return id;
            case 1:
                return username;
            case 2:
                return password;
            case 3:
                return telephone;
        }

        return null;
    }

    @Override
    public int getPropertyCount() {
        return 4;
    }

    @Override
    public void setProperty(int i, Object o) {
        switch(i)
        {
            case 0:
                id = Integer.parseInt(o.toString());
                break;
            case 1:
                username = o.toString();
                break;
            case 2:
                password = o.toString();
                break;
            case 3:
                telephone = o.toString();
                break;
            default:
                break;
        }
    }

    @Override
    public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
        switch(i)
        {
            case 0:
                propertyInfo.type = PropertyInfo.INTEGER_CLASS;
                propertyInfo.name = "id";
                break;
            case 1:
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                propertyInfo.name = "username";
                break;
            case 2:
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                propertyInfo.name = "password";
                break;
            case 3:
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                propertyInfo.name = "telephone";
                break;
            default:
                break;
        }
    }
}

env.getResponse() returns SoapObject, not a User object. env.getResponse()返回SoapObject,而不是User对象。 Its toString() prints 它的toString()打印

anyType{id=0; username=john; password=doe; telephone=074000000000; }

I activated the transport debug and saw that responseDump has the value: 我激活了传输调试,并看到responseDump具有以下值:

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:loginResponse xmlns:ns2="http://ws/"><user><id>0</id><username>john</username><password>doe</password><telephone>074000000000</telephone></user></ns2:loginResponse></S:Body></S:Envelope>

On the server side: 在服务器端:

The WS class: WS类:

    @WebService(serviceName = "LoginWS")
    @Stateless()
    public class LoginWS {

    @EJB
    private LoginService userService;

    @WebMethod(operationName = "login")
    @WebResult(name="user")
    public User login(@WebParam(name = "user") User user) {
        return userService.login(user);
    }

   }

The User class: 用户类:

public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

The webservice is on the Glassfish server from Netbeans. 该Web服务位于Netbeans的Glassfish服务器上。 Its generated wsdl is ( http://w8-pc:8080/LoginWS/LoginWS?wsdl ): 其生成的wsdl是( http:// w8-pc:8080 / LoginWS / LoginWS?wsdl ):

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws/" name="LoginWS">
<types>
<xsd:schema>
<xsd:import namespace="http://ws/" schemaLocation="http://w8-pc:8080/LoginWS/LoginWS?xsd=1"/>
</xsd:schema>
</types>
<message name="register">
<part name="parameters" element="tns:register"/>
</message>
<message name="registerResponse">
<part name="parameters" element="tns:registerResponse"/>
    </message>
    <message name="login">
    <part name="parameters" element="tns:login"/>
    </message>
    <message name="loginResponse">
    <part name="parameters" element="tns:loginResponse"/>
    </message>
    <portType name="LoginWS">
    <operation name="register">
    <input wsam:Action="http://ws/LoginWS/registerRequest" message="tns:register"/>
    <output wsam:Action="http://ws/LoginWS/registerResponse" message="tns:registerResponse"/>
    </operation>
    <operation name="login">
    <input wsam:Action="http://ws/LoginWS/loginRequest" message="tns:login"/>
    <output wsam:Action="http://ws/LoginWS/loginResponse" message="tns:loginResponse"/>
    </operation>
    </portType>
    <binding name="LoginWSPortBinding" type="tns:LoginWS">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="register">
    <soap:operation soapAction=""/>
    <input>
    <soap:body use="literal"/>
    </input>
    <output>
    <soap:body use="literal"/>
    </output>
    </operation>
    <operation name="login">
    <soap:operation soapAction=""/>
    <input>
    <soap:body use="literal"/>
    </input>
    <output>
    <soap:body use="literal"/>
    </output>
    </operation>
    </binding>
    <service name="LoginWS">
    <port name="LoginWSPort" binding="tns:LoginWSPortBinding">
    <soap:address location="http://w8-pc:8080/LoginWS/LoginWS"/>
    </port>
    </service>
    </definitions>

Please tell me what I am doing wrong, I need 请告诉我我在做什么错,我需要

User user = (User) env.getResponse();

to work, not to give a ClassCastException. 工作,而不是给ClassCastException。 I have looked all over the net and StackOverflow, but didn't find an answer.. Please help 我看了遍整个网络和StackOverflow,但是没有找到答案。.请帮助

您必须解析响应并自己构建用户对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM