Web Service Android应用程序:无法序列化1.0

时间:2013-07-11 06:05:58

标签: android web-services serialization ksoap2 android-ksoap2

P.S :我看过类似的问题但却无法理解该怎么做。他们谈论使用元帅课程,我似乎无法理解。

我正在创建一个 Android应用程序使用JAX-WS 。我正在使用 ksoap-2 库。

我从一系列文本字段中获取用户的输入,并将这些输入传递给WebService活动。
在onCreate方法中,我在设置soap对象后调用Web服务:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String cropName = extras.getString(AndroidWSClient.CROP_NAME);
    float area = extras.getFloat(AndroidWSClient.AREA);

同样地,我得到了肥料的一些名称,并将它们添加到名为fertilizerList的ArrayList中。我也以类似的方式得到三个值。

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    // Add parameters to the SOAP object
    // cropName
    PropertyInfo propInfo = new PropertyInfo();
    propInfo.name = "cropName";
    propInfo.type = PropertyInfo.STRING_CLASS;
    request.addProperty(propInfo, cropName);
    System.out.println("!!!! Success with cropName !!!");
    // Area
    propInfo = new PropertyInfo();
    propInfo.setName("area");
    propInfo.setType(Double.class);
    request.addProperty(propInfo, area);
    System.out.println("!!!! Success with area !!!");
    // fertilizerList
    propInfo = new PropertyInfo();
    propInfo.setName("fertilizerList");
    propInfo.setType(ArrayList.class);
    request.addProperty(propInfo, fertilizerList);
    System.out.println("!!!! Success with fertilizerList !!!");
    // N value
    propInfo = new PropertyInfo();
    propInfo.setName("nitrogen");
    propInfo.setType(Integer.class);
    request.addProperty(propInfo, n);
    System.out.println("!!!! Success with N !!!");

在这里,我为它创建了soap对象和属性 我需要将多个参数传递给我的Web服务,因此我添加这些属性的顺序是它们作为Web服务的参数出现的顺序,对吧?
然后我将请求发送到Web服务:

// Send out the request to the web service
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    System.out.println("created envelope!");
    envelope.setOutputSoapObject(request);
    System.out.println("Reached set output object");
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    System.out.println("Reached HttpTransport layer");
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        System.out.println(resultsRequestSOAP.toString());
        textView.setText(resultsRequestSOAP.toString());
        // Set the text view as the activity layout
        setContentView(textView);
    } catch (Exception e) {
        e.printStackTrace();
    } 

以下是logcat的输出:

   07-11 05:37:32.782: I/System.out(1698): !!!! Success with cropName !!!
    07-11 05:37:32.782: I/System.out(1698): !!!! Success with area !!!
    07-11 05:37:32.792: I/System.out(1698): !!!! Success with fertilizerList !!!
    07-11 05:37:32.792: I/System.out(1698): !!!! Success with N !!!
    07-11 05:37:32.872: I/System.out(1698): created envelope!
    07-11 05:37:32.872: I/System.out(1698): Reached set output object
    07-11 05:37:32.912: I/System.out(1698): Reached HttpTransport layer
    07-11 05:37:33.012: W/System.err(1698): java.lang.RuntimeException: Cannot serialize: 1.0
    07-11 05:37:33.012: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:629)
    07-11 05:37:33.022: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:613)
    07-11 05:37:33.032: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
    07-11 05:37:33.032: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
    07-11 05:37:33.062: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
    07-11 05:37:33.072: W/System.err(1698):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
    07-11 05:37:33.072: W/System.err(1698):     at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
    07-11 05:37:33.092: W/System.err(1698):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
    07-11 05:37:33.092: W/System.err(1698):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
    07-11 05:37:33.112: W/System.err(1698):     at com.example.androidwsclient.WebServiceActivity.onCreate(WebServiceActivity.java:99)
    07-11 05:37:33.112: W/System.err(1698):     at android.app.Activity.performCreate(Activity.java:5104)
    07-11 05:37:33.132: W/System.err(1698):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

1 个答案:

答案 0 :(得分:4)

为Double实施Marshal

public class MarshalDouble implements Marshal 
{

@Override
public Object readInstance(XmlPullParser parser, String namespace, String name, 
        PropertyInfo expected) throws IOException, XmlPullParserException {

    return Double.parseDouble(parser.nextText());
}

public void register(SoapSerializationEnvelope cm) {
     cm.addMapping(cm.xsd, "double", Double.class, this);

}

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

然后在代码中调用There寄存器方法。

new MarshalDouble().register(envelope);