Android Ksoap2 aceita Float ?
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());
}
}
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());
}
}
Gabriel Chiarelo
Curtidas 0
Melhor post
Wolney Dias
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]
[url]http://stackoverflow.com/questions/6608701/android-how-to-pass-double-value-to-service-using-ksoap[/url]
GOSTEI 1
Mais Respostas
Wolney Dias
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]
[url]http://stackoverflow.com/questions/6608701/android-how-to-pass-double-value-to-service-using-ksoap[/url]
GOSTEI 0
Gabriel Chiarelo
14/03/2016
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());
}
}
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());
}
}
GOSTEI 0
Wolney Dias
14/03/2016
Então está resolvido?
GOSTEI 0
Gabriel Chiarelo
14/03/2016
Sim, valeu!
GOSTEI 0
Wolney Dias
14/03/2016
Disponha amigo.
GOSTEI 0