Android Ksoap2 aceita Float ?

14/03/2016

1

Olá,

Gostaria de saber se a biblioteca Ksoap2 aceita Float? pois estou com o seguinte cenário:

Estou executando um método no webservice:

@WebMethod(operationName = "calcula")
public Float calcula(@WebParam(name = "x") Float x, @WebParam(name = "y") Float y, @WebParam(name = "op") String op) {
//TODO write your implementation code here:

switch(op){
case "+": return x + y;
case "-": return x - y;
case "*": return x * y;
case "/": return x / y;
}
return null;
}

Porem na class do aplicativo Android os parâmetros que são float ocorre um erro ao executar "httpTrans.call("calcula", envelope);" gerando a seguinte exception: java.lang.RuntimeException: Cannot serialize: 1.0

Código fonte da class:

package br.com.unifacef.consomeandriod2;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

/**
* Created by gabriel on 11/03/16.
*/
public class Calculadora {

public Calculadora(){

}

public Float calcula(Float x, Float y, String op) throws IOException, XmlPullParserException {

SoapObject soap = new SoapObject("http://provedor/", "calcula");

PropertyInfo pi1 = new PropertyInfo();
pi1.setName("x");
pi1.setValue(x);
pi1.setType(Float.class);
soap.addProperty(pi1);

PropertyInfo pi2 = new PropertyInfo();
pi2.setName("y");
pi2.setValue(y);
pi2.setType(Float.class);
soap.addProperty(pi2);

PropertyInfo pi3 = new PropertyInfo();
pi3.setName("op");
pi3.setValue(op);
pi3.setType(op.getClass());
soap.addProperty(pi3);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;

envelope.setOutputSoapObject(soap);

MarshalDouble m = new MarshalDouble();
m.register(envelope);

HttpTransportSE httpTrans = new HttpTransportSE("http://192.168.1.98:8080/ProvedorWSCalculadora/Calculadora?WSDL");

httpTrans.call("calcula", envelope);

Object resultado = envelope.getResponse();

return Float.parseFloat(resultado.toString());

}

}
Responder

Post mais votado

14/03/2016

[url]http://stackoverflow.com/questions/6111169/androidksoap-issue[/url]

[url]http://stackoverflow.com/questions/6608701/android-how-to-pass-double-value-to-service-using-ksoap[/url]
Responder

Mais Posts

14/03/2016

Wolney Dias

[url]http://stackoverflow.com/questions/6111169/androidksoap-issue[/url]

[url]http://stackoverflow.com/questions/6608701/android-how-to-pass-double-value-to-service-using-ksoap[/url]
Responder
Valeu, já até tinha visto um dos links:

Eu me espelhei nos exemplos é meu código fonte funcionando ficou assim:


public class Calculadora {

public Calculadora(){

}

public Float calcula(Float x, Float y, String op) throws IOException, XmlPullParserException {

SoapObject soap = new SoapObject("http://provedor/", "calcula");

soap.addProperty("x",x);
soap.addProperty("y",y);

PropertyInfo pi3 = new PropertyInfo();
pi3.setName("op");
pi3.setValue(op);
pi3.setType(op.getClass());
soap.addProperty(pi3);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(soap);

MarshalFloat m = new MarshalFloat();
m.register(envelope);

HttpTransportSE httpTrans = new HttpTransportSE("http://192.168.1.98:8080/ProvedorWSCalculadora/Calculadora?WSDL");

httpTrans.call("calcula", envelope);

Object resultado = envelope.getResponse();

return Float.parseFloat(resultado.toString());

}
}



public class MarshalFloat implements Marshal {

public Object readInstance(XmlPullParser parser, String namespace, String name,
PropertyInfo expected) throws IOException, XmlPullParserException {
return Float.parseFloat(parser.nextText());
}

public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "float", Float.class, this);
}

public void writeInstance(XmlSerializer writer, Object obj) throws IOException{
writer.text(obj.toString());
}

}
Responder

16/03/2016

Wolney Dias

Então está resolvido?
Responder
Sim, valeu!
Responder

16/03/2016

Wolney Dias

Disponha amigo.
Responder