解析ksoap2响应中的复杂类型时出现ClassCastException

时间:2014-10-30 13:05:25

标签: parsing response ksoap2

我一直在搜索很多类似的主题,但没有一个能适合我的情况。我知道我的复杂类需要实现KvmSerializable。我还在soap信封中添加了类映射。但仍有ClassCastException。任何人都可以帮助指出我的代码有什么问题吗?

Web服务签名:CUser CreateRegisteredUser();

WSDL:



<wsdl:definitions xmlns:tns="http://192.168.5.6/" targetNamespace="http://192.168.5.6/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <wsdl:types>
        <s:schema targetNamespace="http://192.168.5.6/">
            <s:complexType name="CUser">
                <s:annotation>
                    <s:documentation>
                        User information
                    </s:documentation>
                </s:annotation>
                <s:sequence>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="userToken" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                User token
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="false" name="userRole" type="s:int">
                        <s:annotation>
                            <s:documentation>
                                User role
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="false" name="userState" type="s:int">
                        <s:annotation>
                            <s:documentation>
                                User state
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="userName" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                User name
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="password" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                Password
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="email" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                Email
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="mobilePhone" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                Mobile phone #
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="false" name="expr" type="s:int">
                        <s:annotation>
                            <s:documentation>
                                Experience
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="false" name="points" type="s:int">
                        <s:annotation>
                            <s:documentation>
                                Points
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="registerDate" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                Registered date
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="false" name="gender" type="s:int">
                        <s:annotation>
                            <s:documentation>
                                Gender
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                    <s:element minOccurs="1" maxOccurs="1" nillable="true" name="location" type="s:string">
                        <s:annotation>
                            <s:documentation>
                                Location
                            </s:documentation>
                        </s:annotation>
                    </s:element>
                </s:sequence>
            </s:complexType>
        </s:schema>
    </wsdl:types>
    <wsdl:message name="UserManagerSoapIn" />
    <wsdl:message name="UserManagerSoapOut" />
    <wsdl:message name="CreateAnonymousUserSoapIn" />
    <wsdl:message name="CreateAnonymousUserSoapOut">
        <wsdl:part name="return" type="s:string">
            <s:documentation>
                user token
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="CreateRegisteredUser2SoapIn">
        <wsdl:part name="user" type="tns:CUser">
            <s:documentation>
                User information to register
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="CreateRegisteredUser2SoapOut">
        <wsdl:part name="return" type="tns:CUser">
            <s:documentation>
                User information registered. Null indicates failures
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="CreateRegisteredUserSoapIn" />
    <wsdl:message name="CreateRegisteredUserSoapOut">
        <wsdl:part name="return" type="tns:CUser">
            <s:documentation>
                User information registered. Null indicates failures
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="ValidateAnonymousUserSoapIn">
        <wsdl:part name="user_token" type="s:string">
            <s:documentation>
                user token
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="ValidateAnonymousUserSoapOut">
        <wsdl:part name="return" type="s:boolean">
            <s:documentation>
                result
            </s:documentation>
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="UserManagerSoap">
        <wsdl:operation name="UserManager">
            <wsdl:documentation>
                Constructor
            </wsdl:documentation>
            <wsdl:input message="tns:UserManagerSoapIn" />
            <wsdl:output message="tns:UserManagerSoapOut" />
        </wsdl:operation>
        <wsdl:operation name="CreateAnonymousUser">
            <wsdl:documentation>
                CreateAnonymousUser
            </wsdl:documentation>
            <wsdl:input message="tns:CreateAnonymousUserSoapIn" />
            <wsdl:output message="tns:CreateAnonymousUserSoapOut" />
        </wsdl:operation>
        <wsdl:operation name="CreateRegisteredUser2">
            <wsdl:documentation>
                CreateRegisteredUser2
            </wsdl:documentation>
            <wsdl:input message="tns:CreateRegisteredUser2SoapIn" />
            <wsdl:output message="tns:CreateRegisteredUser2SoapOut" />
        </wsdl:operation>
        <wsdl:operation name="CreateRegisteredUser">
            <wsdl:documentation>
                CreateRegisteredUser
            </wsdl:documentation>
            <wsdl:input message="tns:CreateRegisteredUserSoapIn" />
            <wsdl:output message="tns:CreateRegisteredUserSoapOut" />
        </wsdl:operation>
        <wsdl:operation name="ValidateAnonymousUser">
            <wsdl:documentation>
                ValidateAnonymousUser
            </wsdl:documentation>
            <wsdl:input message="tns:ValidateAnonymousUserSoapIn" />
            <wsdl:output message="tns:ValidateAnonymousUserSoapOut" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="UserManagerSoap" type="tns:UserManagerSoap">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
        <wsdl:operation name="UserManager">
            <soap:operation soapAction="http://192.168.5.6/UserManager" />
            <wsdl:input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="CreateAnonymousUser">
            <soap:operation soapAction="http://192.168.5.6/CreateAnonymousUser" />
            <wsdl:input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="return" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="CreateRegisteredUser2">
            <soap:operation soapAction="http://192.168.5.6/CreateRegisteredUser2" />
            <wsdl:input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="user" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="return" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="CreateRegisteredUser">
            <soap:operation soapAction="http://192.168.5.6/CreateRegisteredUser" />
            <wsdl:input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="return" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="ValidateAnonymousUser">
            <soap:operation soapAction="http://192.168.5.6/ValidateAnonymousUser" />
            <wsdl:input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="user_token" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.5.6/" parts="return" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="UserManager">
        <wsdl:port name="UserManagerSoap" binding="tns:UserManagerSoap">
            <soap:address location="https://192.168.5.6/user_service.php" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
&#13;
&#13;
&#13;

ksoap2回应。 :

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://192.168.5.6/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
  <env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">
    <ns1:CreateRegisteredUserResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
      <rpc:result>return</rpc:result>
      <return xsi:type="ns1:CUser">
        <userToken xsi:type="xsd:string">token</userToken>
        <userRole xsi:type="xsd:int">1</userRole>
        <userState xsi:type="xsd:int">2</userState>
        <userName xsi:type="xsd:string">aaa</userName>
        <password xsi:type="xsd:string">123456</password>
        <email xsi:type="xsd:string">aaa@aaa.com</email>
        <mobilePhone xsi:type="xsd:string">15201193843</mobilePhone>
        <expr xsi:type="xsd:int">0</expr>
        <points xsi:type="xsd:int">0</points>
        <registerDate xsi:type="xsd:string">registerDate</registerDate>
        <gender xsi:type="xsd:int">0</gender>
        <location xsi:type="xsd:string">location</location>
      </return>
    </ns1:CreateRegisteredUserResponse>
  </env:Body>
</env:Envelope>
&#13;
&#13;
&#13;

我的CUser课程:

&#13;
&#13;
public class CUser implements Serializable, KvmSerializable {
/**
 * 
 */
private static final long serialVersionUID = -4000559522164150752L;
protected UUID userToken;
protected UserRole userRole;
protected UserState userState;
protected String userName;
protected String password;
protected String email;
protected String mobilePhone;
protected int expr;
protected int points;
protected String registerDate;
protected UserGender gender;
protected String location;

public CUser () {
	this.userRole = UserRole.Unknown;
	this.userState = UserState.Unknown;
	this.gender = UserGender.Unknown;
	this.expr = this.points = 0;
}

public CUser (UUID token) {
	this.userToken = token;
	this.userRole = UserRole.Anonymous;
	this.userState = UserState.Unknown;
	this.gender = UserGender.Unknown;
	this.expr = this.points = 0;
}

public CUser (String name, String password) {
	this.userName = name;
	this.password = password;
	this.userRole = UserRole.Unknown;
	this.userState = UserState.Unknown;
	this.gender = UserGender.Unknown;
	this.expr = this.points = 0;
}

public CUser (
		UUID token, 
		UserRole role, 
		UserState state, 
		String name, 
		String password, 
		String email, 
		String phoneNumber, 
		int expr,
		int points,
		String registerDate,
		UserGender gender) {
	this.userToken = token;
	this.userRole = role;
	this.userState = state;
	this.userName = name;
	this.password = password;
	this.email = email;
	this.mobilePhone = phoneNumber;
	this.expr = expr;
	this.points = points;
	this.registerDate = registerDate;
	this.gender = gender;
}

public UUID getToken() {return this.userToken;}
public UserRole getRole() {return this.userRole;}
public UserState getState() {return this.userState;}
public String getName() {return this.userName;}
public String getPassword() {return this.password;}
public String getEmail() {return this.email;}
public String getPhoneNumber() {return this.mobilePhone;}
public int getExperience() {return this.expr;}
public int getPoints() {return this.points;}
public String getRegisterDate() {return this.registerDate;}
public UserGender getGender() {return this.gender;}

public void setName(String name) {this.userName = name;}
public void setGender(UserGender gender) {this.gender = gender;}

private void writeObject(ObjectOutputStream oos) throws IOException {
	// default serialization
	oos.defaultWriteObject();

	oos.writeObject(this.userToken);
	oos.write(this.userRole.getValue());
	oos.writeUTF(this.userName == null ? "" : this.userName);
	oos.writeUTF(this.password == null ? "" : this.password);
}

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
	// default deserialization
	ois.defaultReadObject();
	
	this.userToken = (UUID) ois.readObject();
	this.userRole = UserRole.fromInt(ois.readInt());
	
	try {
		this.userName = ois.readUTF();
		this.password = ois.readUTF();
	} catch (EOFException e) {
		this.userName = null;
		this.password = null;
	}
}

@Override
public Object getProperty(int arg0) {
	switch (arg0) {
	case 0:
		return this.userToken == null ? null : this.userToken.toString();
	case 1:
		return this.userRole.getValue();
	case 2:
		return this.userState.getValue();
	case 3:
		return this.userName;
	case 4:
		return this.password;
	case 5:
		return this.email;
	case 6:
		return this.mobilePhone;
	case 7:
		return this.expr;
	case 8:
		return this.points;
	case 9:
		return this.registerDate;
	case 10:
		return this.gender.getValue();
	case 11:
		return this.location;
	}
	return null;
}

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

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
	switch (arg0) {
	case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "userToken";
        break;
	case 1:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "userRole";
        break;
	case 2:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "userState";
        break;
	case 3:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "userName";
        break;
	case 4:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "password";
        break;
	case 5:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "email";
        break;
	case 6:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "mobilePhone";
        break;
	case 7:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "expr";
        break;
	case 8:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "points";
        break;
	case 9:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "registerDate";
        break;
	case 10:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "gender";
        break;
	case 11:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "location";
        break;
	}		
}

@Override
public void setProperty(int arg0, Object value) {
	switch (arg0) {
	case 0:
		this.userToken = UUID.fromString(value.toString());
		break;
	case 1:
		this.userRole = UserRole.fromInt(Integer.parseInt(value.toString()));
		break;
	case 2:
		this.userState = UserState.fromInt(Integer.parseInt(value.toString()));
		break;
	case 3:
		this.userName = value.toString();
		break;
	case 4:
		this.password = value.toString();
		break;
	case 5:
		this.email = value.toString();
		break;
	case 6:
		this.mobilePhone = value.toString();
		break;
	case 7:
		this.expr = Integer.parseInt(value.toString());
		break;
	case 8:
		this.points = Integer.parseInt(value.toString());
		break;
	case 9:
		this.registerDate = value.toString();
		break;
	case 10:
		this.gender = UserGender.fromInt(Integer.parseInt(value.toString()));
		break;
	case 11:
		this.location = value.toString();
		break;
	}		
}
}
&#13;
&#13;
&#13;

调用Web服务的代码:

&#13;
&#13;
	public CUser CreateRegisteredUser(String userName, String password, String email, UserGender gender) throws SoapFault, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException, InterruptedException, ExecutionException {
		String METHOD_NAME = "CreateRegisteredUser";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
		CUser user = new CUser(
				currentUserToken, 
				UserRole.Registered, 
				UserState.Inactive, 
				userName, 
				password,
				email,
				null,
				0,
				0,
				null, 
				gender);
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.setOutputSoapObject(request);
        
        envelope.addMapping(NAMESPACE, "CUser", new CUser().getClass());
        
        Object soapResult = InvokeSoapMethed(request, envelope);

        if (soapResult == null) {
        	Log.v("UserManagerSoapClient.CreateRegisteredUser", "null");
        	return null;
        } else {
        	SoapObject soapObj = (SoapObject) envelope.bodyIn;
        	user = (CUser) soapObj.getProperty(1); // java.lang.ClassCastException
    		return null;
        }	
	}


	
	protected Object InvokeSoapMethed(SoapObject request, SoapSerializationEnvelope envelope) throws SoapFault, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException, InterruptedException, ExecutionException {
		final String SOAP_ACTION = this.NAMESPACE + request.getName();    
		Log.v("Ksoap2WSBase.InvokeSoapMethed", "SOAP_ACTION: " + SOAP_ACTION);

        SoapAsyncTask task = null;
        Object result = null;
        if (envelope == null) {
        	envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.setOutputSoapObject(request);
        }
        
		task = new SoapAsyncTask(this.CreateTransport());
		task.execute(envelope, SOAP_ACTION);

      	result = task.get();
		if (result instanceof SoapFault) {
			throw (SoapFault) result;
		}
		
        if (result == null) {
			Log.v("Ksoap2WSBase.InvokeSoapMethed", "Null soap result returned");
        	return null;
        }

        DumpSoapResult((SoapObject) result);
		return result;	
	}

public class SoapAsyncTask extends AsyncTask<Object, Void, Object> {

	protected Transport androidTransport;
	
	public SoapAsyncTask(Transport androidTransport) {
		this.androidTransport = androidTransport;
	}
	
	@Override
	// args[0] = (SoapSerializationEnvelope) envelope
	// args[1] = (String) soap_action
	protected Object doInBackground(Object... args) {
		Log.v("SoapAsyncTask.doInBackground", String.format("SoapAction: %s", args[1]));
		
		Object result = null;
        
		try {
			SoapSerializationEnvelope envelope = (SoapSerializationEnvelope) args[0];
			androidTransport.call((String)args[1], envelope);
			result = envelope.bodyIn;
			
			Log.v("SoapAsyncTask.doInBackground", "Dump Request:" + androidTransport.requestDump);
			Log.v("SoapAsyncTask.doInBackground", "Dump Response:" + androidTransport.responseDump);
		} catch (HttpResponseException e) {
			Log.v("SoapAsyncTask.doInBackground", "HttpResponseException");
			e.printStackTrace();
		} catch (IOException e) {
			Log.v("SoapAsyncTask.doInBackground", "IOException");
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			Log.v("SoapAsyncTask.doInBackground", "XmlPullParserException");
			e.printStackTrace();
		} catch (Exception e) {
			Log.v("SoapAsyncTask.doInBackground", "Exception");
			e.printStackTrace();
		}
		
		return result;
	}

}
&#13;
&#13;
&#13;

日志:

&#13;
&#13;
Propert[0]: return

Propert[1]: CUser{userToken=token; userRole=1; userState=2; userName=aaa; password=123456; email=aaa@aaa.com; mobilePhone=15201193843; expr=0; points=0; registerDate=registerDate; gender=0; location=location; }
&#13;
&#13;
&#13;

0 个答案:

没有答案