来自ksoap2-android响应的ClassCastException

时间:2016-09-07 20:44:43

标签: java android web-services ksoap2 android-ksoap2

我是Java和Android的初学者,并且因为以下问题而在一天中的大部分时间都在努力:

我使用kso​​ap2从webservice调用登录方法。我希望得到一个User对象作为响应,而不是SoapObject。

来自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);

    ......

Android用户类是:

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()返回SoapObject,而不是User对象。 它的toString()打印

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

我激活了传输调试,发现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>

在服务器端:

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);
    }

   }

用户类:

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;
    }

Web服务位于Netbeans的Glassfish服务器上。它生成的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>

请告诉我我做错了什么,我需要

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

工作,而不是给出ClassCastException。 我已经遍布网络和StackOverflow,但没有找到答案.. 请帮忙

1 个答案:

答案 0 :(得分:0)

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