在android中使用kso​​ap2的java.lang.ClassCastException

时间:2012-09-27 15:45:52

标签: android web-services soap android-ksoap2

我正在使用Eclipse开发android并尝试连接到.Net webservice。我正在使用kso​​ap2库来调用webservice但是在调用一个返回给我任意对象的方法时我遇到了这个异常:

09-27 16:29:59.641: W/System.err(312): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject

返回一个布尔或整数的方法可以正常工作,但这会给我带来麻烦。 以下是服务器的响应:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getUsuarioResponse xmlns="http://www.saintek.cl/">
<getUsuarioResult>
<EntityKey>
<EntitySetName>Usuarios</EntitySetName>
<EntityContainerName>ModelNeearsContainer</EntityContainerName>
<EntityKeyValues>
<EntityKeyMember>
<Key>Id</Key>
<Value xsi:type="xsd:int">7</Value>
</EntityKeyMember>
</EntityKeyValues>
</EntityKey>
<Id>7</Id>
<ReputacionId xsi:nil="true" />
<FotoId xsi:nil="true" />
<CoordenadasId xsi:nil="true" />
<FechaNacimiento>1900-01-01T00:00:00</FechaNacimiento>
<FechaRegistro>2012-09-21T16:46:07.707</FechaRegistro>
<Telefono />
<Reputacion_Id xsi:nil="true" />
<Habilitado>false</Habilitado>
<CoordenadaReference />
<FotoReference />
<ReputacionReference />
</getUsuarioResult>
</getUsuarioResponse>
</soap:Body>
</soap:Envelope>

这是我的代码:

    String strJson = "";

    String usuario = ((EditText)findViewById(R.id.editText_mail_entrar)).getText().toString();
    String password = ((EditText)findViewById(R.id.editText_password_entrar)).getText().toString();

    request = new SoapObject(WebServiceConstants.NAMESPACE, METHOD_NAME);

    //---le agregamos los parametros---
    request.addProperty("usuario", usuario);
    request.addProperty("password", password);

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true; //por que es .NET

    envelope.setOutputSoapObject(request);
    //envelope.addMapping(WebServiceConstants.NAMESPACE, "Usuario", new Usuario().getClass());

    HttpTransportSE transporte = new HttpTransportSE(WebServiceConstants.URL);
    transporte.debug = true;

    try {

        transporte.call(SOAP_ACTION, envelope);
        resultsRequestSoap = (SoapPrimitive)envelope.getResponse();

    } catch (IOException e) {

        e.printStackTrace();

    } catch (XmlPullParserException e) {

        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    strJson = resultsRequestSoap.toString();

1 个答案:

答案 0 :(得分:1)

将resultsRequestSoap声明为Object类的实例,并尝试不使用类转换,如下所示

 resultsRequestSoap = envelope.getResponse();