org.ksoap2.serialization.SoapPrimitive无法转换为org.ksoap2.serialization.SoapObject

时间:2018-11-18 19:23:51

标签: android

我在使用Oracle数据库的NetBeans中拥有一个Web服务,而当我在Android中进行选择时,我会遇到这种情况

[Link to second file][2]<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

这是WebService的代码:

org.ksoap2.serialization.SoapObject cannot be cast to org.ksoap2.serialization.SoapPrimitive

这是来自Android Studio的代码:

@WebMethod(operationName = "mostarNombre")
public String mostarNombre(@WebParam(name = "id") int id) {
    String nomb = "";
    try {
        DriverManager.registerDriver(new OracleDriver());
        Connection cn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:xxxx:xx", "SYSTEM", "xxxx");

        PreparedStatement consultaEmp = cn.prepareStatement("select nombre from ejemplo where id=?");
        consultaEmp.setInt(1, id);

        ResultSet rs = consultaEmp.executeQuery();
        if (rs.next()) {
            nomb = rs.getString("nombre");
        } else {
            nomb = "No encontrado";
        }

    } catch (SQLException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }

    return nomb;
}

此行中是例外:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();

        StrictMode.setThreadPolicy(policy);

        String resultadoFINAL;
        //Creacion de la Solicitud
        SoapObject request = new SoapObject(NAMESPACE, METHOD2);
        //Creacion del Envelope
        SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        sobre.setOutputSoapObject(request);
        //Creacion del transporte
        HttpTransportSE transporte = new HttpTransportSE(URL);

        String texto = this.etId.getText().toString();

        // Paso de parámetro
        PropertyInfo numeroEmp = new PropertyInfo();
        numeroEmp.setName("numero");
        numeroEmp.setValue(texto);
        numeroEmp.setType(Integer.class);
        request.addProperty(numeroEmp);

        //Llamada
        transporte.call(SOAPACTION2, sobre);

        //Resultado
        SoapObject resultado = (SoapObject) sobre.getResponse();
        resultadoFINAL = "Nombre: "+resultado.toString();
        tvConsulta.setText(resultadoFINAL);

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

我是WebService的新手,所以感谢所有帮助!!! :)

编辑:

我尝试过:

SoapObject resultado = (SoapObject) sobre.getResponse();

此:

SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse();

并以此:

SoapObject resultado = (SoapObject) sobre.getResponse();
resultadoFINAL = "Nombre: "+resultado.getProperty(0).toString();

有了对象,我会得到“无限制”,就像在此循环中ResultSet没有next()一样:

Object resultado = (Object) sobre.getResponse();

1 个答案:

答案 0 :(得分:1)

它可能应该是:

SoapObject resultado = (SoapObject) sobre.getResponse();

...为了避免这种可能的强制转换异常。