0

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

我正在创建一个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);

类似地,我得到一些肥料的名称并将它们添加到一个称为肥料列表的 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 !!!");

在这里,我创建了肥皂对象和属性。
我需要将多个参数传递给我的 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)
4

1 回答 1

5

为双重执行元帅

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 register 方法。

new MarshalDouble().register(envelope);
于 2013-07-17T04:55:24.913 回答