【问题标题】:java.lang.ClassCastException using ksoap2 in androidjava.lang.ClassCastException 在 android 中使用 ksoap2
【发布时间】:2012-09-27 15:45:51
【问题描述】:

我正在使用 Eclipse 通过 android 进行开发并尝试连接到 .Net Web 服务。我正在使用 ksoap2 库来调用 web 服务,但在调用返回任意对象的方法时出现此异常:

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

【问题讨论】:

    标签: android web-services soap android-ksoap2


    【解决方案1】:

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

     resultsRequestSoap = envelope.getResponse();
    

    【讨论】:

    • 那么我如何将该对象转换为字符串?
    • 因为当我将其转换为 SoapPrimitive 时,我可以使用 .toString() 方法将 resultsRequestSoap 转换为字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多