Web Service Android Application : Cannot serialize 1.0

こ雲淡風輕ζ 提交于 2019-12-01 13:15:17

问题


P.S : I have looked at similar questions but haven't been able to understand what to do. They talk about using a marshal class, which I can't seem to understand.

I am creating an Android application to consume a JAX-WS. I am using the ksoap-2 library for the same.

I take the inputs from the user in a series of text fields and pass these onto WebService activity.
Here in the onCreate method I call the web service after setting up the soap object :

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);

Similarly I get the some names of fertilizers and add them to an ArrayList called fertilizerList. i also get three more values in a similar manner.

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 !!!");

Here I create the soap object and properties to it.
I need to pass multiple parameters to my Web Service, so the order in which I add these properties is the order in which they'll appear as parameters to the web service, right ?
I then send out the request to the web service :

// 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();
    } 

Here is the output from the 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:


Implement a Marshal for the Double

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());
    }           
}

Then call There register method in your code.

new MarshalDouble().register(envelope);


来源:https://stackoverflow.com/questions/17586152/web-service-android-application-cannot-serialize-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!