我正在尝试进行SOAP解析,但收到错误。在第transport.call(SOAP_ACTION, envelope);
行中捕获异常时,得到了例外:Cannot Serialize
代码:
private double getFahrenheit(double celsius) throws IOException, XmlPullParserException {
double farenheit = 0.0;
SoapObject request =new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo info = new PropertyInfo();
SoapPrimitive primitive;
info.setName("Celsius");
info.setValue(celsius);
info.setType(double.class);
request.addProperty(info);
SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
new MarshalBase64().register(envelope);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, envelope);
primitive = (SoapPrimitive) envelope.getResponse();
farenheit = Double.parseDouble(primitive.toString());
return farenheit;
}
我收到此错误:
01-15 14:53:29.745: E/AndroidRuntime(2130): FATAL EXCEPTION: AsyncTask #1
01-15 14:53:29.745: E/AndroidRuntime(2130): Process: com.example.demosoapparsing1, PID: 2130
01-15 14:53:29.745: E/AndroidRuntime(2130): java.lang.RuntimeException: An error occured while executing doInBackground()
01-15 14:53:29.745: E/AndroidRuntime(2130): at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-15 14:53:29.745: E/AndroidRuntime(2130): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.lang.Thread.run(Thread.java:841)
01-15 14:53:29.745: E/AndroidRuntime(2130): Caused by: java.lang.RuntimeException: Cannot serialize: 55.0
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:629)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:613)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
01-15 14:53:29.745: E/AndroidRuntime(2130): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
01-15 14:53:29.745: E/AndroidRuntime(2130): at com.example.demosoapparsing1.MainActivity.getFahrenheit(MainActivity.java:101)
01-15 14:53:29.745: E/AndroidRuntime(2130): at com.example.demosoapparsing1.MainActivity.access$1(MainActivity.java:77)
01-15 14:53:29.745: E/AndroidRuntime(2130): at com.example.demosoapparsing1.MainActivity$CalculateAsync.doInBackground(MainActivity.java:157)
01-15 14:53:29.745: E/AndroidRuntime(2130): at com.example.demosoapparsing1.MainActivity$CalculateAsync.doInBackground(MainActivity.java:1)
01-15 14:53:29.745: E/AndroidRuntime(2130): at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-15 14:53:29.745: E/AndroidRuntime(2130): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
答案 0 :(得分:0)
对于info.setType(double.class);你应该注册marshaller。 见fe。这个链接How to serialize double value using kSOAP in Android