Я новичок в Java и Android и боролись в течение большей части дня со следующей проблемой:ClassCastException от ksoap2-андроида ответ
Я использую ksoap2 вызвать метод входа из веб-сервиса. Я ожидаю получить объект 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);
......
Андроида класса Пользователь:
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, а не объект пользователя. Его 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;
}
Веб-сервис находится на сервере Glassfish от Netbeans. Его генерируется 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, но не нашел ответа. Пожалуйста, помогите