将对象数组发送到WebService

时间:2015-11-26 11:28:05

标签: java android web-services

我想向WebService发送一个对象数组,但是我有 RuntimeException:无法序列化[myClass] 。我尝试了许多创建请求的变体,但没有一个有效。

我的班级调用WebService:

   SoapObject request = new SoapObject(NAMESPACE, NAME);

    com.gt.android.webservice.model.LocationRecord[] table = convertObjects(locationRecords).toArray(new com.gt.android.webservice.model.LocationRecord[locationRecords.size()]);

    SoapObject root = new SoapObject(NAMESPACE, "locRecord");

    for (com.gt.android.webservice.model.LocationRecord i : table){
        root.addProperty("LocationRecord", i);
    }

    request.addSoapObject(root);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);

    envelope.addMapping(NAMESPACE, "LocationRecord", com.gt.android.webservice.model.LocationRecord.class);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION + NAME, envelope);
... }

首先:我必须从我的列表从一个类转换为另一个类(只是重写字段(以实现平面结构),稍后我尝试创建我的根,添加每个字段,添加序列化,请求,映射和在至少发送我的请求。这是webService结构:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SynchonizeLocations xmlns="http://tempuri.org/">
      <locRecord>
        <LocationRecord>
          <id>int</id>
          <logTime>dateTime</logTime>
          <message>string</message>
          <lt>double</lt>
          <lg>double</lg>
          <timestamp>dateTime</timestamp>
          <sessionBestLocation>boolean</sessionBestLocation>
          <number1>int</number1>
          <number2>int</number2>
        </LocationRecord>
        <LocationRecord>
          <id>int</id>
          <logTime>dateTime</logTime>
          <message>string</message>
          <lt>double</lt>
          <lg>double</lg>
          <timestamp>dateTime</timestamp>
          <sessionBestLocation>boolean</sessionBestLocation>
          <number1>int</number1>
          <number2>int</number2>
        </LocationRecord>
      </locRecord>
    </SynchonizeLocations>
  </soap:Body>
</soap:Envelope>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用Gson并将对象转换为字符串。

  1. 将包含对象数组的新对象类编写为字段。
  2. 创建新对象并将数组加载到其中。
  3. 使用Gson将对象转换为String:

    Gson gson = new Gson();
       String json = gson.toJson(yourObject);

  4. 示例:

    public class LocationRecordsHolder {
    
        public LocationRecord[] table;
    
        public LocationRecordsHolder(LocationRecord[] table) {
            this.table = table;
        }
    
        // Convert object to String
        public static String converToString(LocationRecordHolder obj) {
            Gson gson = new Gson();
            return gson.toJson(obj);
        }
    
        // Conver String to object
        public static LocationRecordHolder convertFromString(String json) {
            Gson gson = new Gson();
            return gson.fromJson(br, LocationRecordsHolder.class);
        }
    }