我正在尝试从Android客户端向我的WCF服务发送一系列复杂对象。 我使用http://seesharpgears.blogspot.ru/2010/10/web-service-that-returns-array-of.html作为接收复杂对象数组的示例,并且工作正常。但是我无法理解如何发送这个数组。
我正在使用kmvserializable作为对象: 公共类RatingHttps实现KvmSerializable {
public int CIsso;
public int RatingIsso;
public long RatingDate;
public String RatingExt;
public RatingHttps() {super();}
public RatingHttps(int CIsso, int RatingIsso, long RatingDate, String RatingExt) {
this.CIsso = CIsso;
this.RatingIsso = RatingIsso;
this.RatingDate = RatingDate;
this.RatingExt = RatingExt;
}
@Override
public Object getProperty(int i) {
Object property = null;
switch (i) {
case 0:
property = this.CIsso;
break;
case 1:
property = this.RatingDate;
break;
case 2:
property = this.RatingExt;
break;
case 3:
property = this.RatingIsso;
break;
}
return property;
}
@Override
public int getPropertyCount() {
return 4;
}
@Override
public void setProperty(int i, Object o) {
switch (i) {
case 0:
this.CIsso = Integer.parseInt(o.toString());
break;
case 1:
this.RatingDate = Long.parseLong(o.toString());
break;
case 2:
this.RatingExt = o.toString();
break;
case 3:
this.RatingIsso = Integer.parseInt(o.toString());
break;
}
}
private static final String NAMESPACE = "http://schemas.datacontract.org/2004/07/Ais7UpdateServer";
@Override
public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
switch (i) {
case 0:
propertyInfo.type = PropertyInfo.INTEGER_CLASS;
propertyInfo.name = "CIsso";
propertyInfo.setNamespace(NAMESPACE);
break;
case 1:
propertyInfo.type = PropertyInfo.LONG_CLASS;
propertyInfo.name = "RatingDate";
propertyInfo.setNamespace(NAMESPACE);
break;
case 2:
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "RatingExt";
propertyInfo.setNamespace(NAMESPACE);
break;
case 3:
propertyInfo.type = PropertyInfo.INTEGER_CLASS;
propertyInfo.name = "RatingIsso";
propertyInfo.setNamespace(NAMESPACE);
break;
default: break;
}
}
这就是我使用它的方式:
List<RatingHttps> ratings = new ArrayList<>();
RatingHttps rating = new RatingHttps();
rating.CIsso = 1;
rating.RatingIsso = 2;
rating.RatingExt = "LOL";
rating.RatingDate = 3;
ratings.add(rating);
rating = new RatingHttps();
rating.CIsso = 10;
rating.RatingIsso = 20;
rating.RatingExt = "OLOLO";
rating.RatingDate = 30;
ratings.add(rating);
//cr.moveToNext();
//request = GetSoapObject(METHOD_NAME[2]);
request = new SoapObject(NAMESPACE, METHOD_NAME[2]);
//envelope = GetEnvelope(request);
request.addProperty("id", resultId.toString());
//PropertyInfo pi = new PropertyInfo();
//pi.setName("ratings");
//pi.setValue(ratings);
//pi.setType(ratings.getClass());
//request.addProperty(pi);
request.addProperty("ratings", ratings);
//envelope.addMapping(NAMESPACE, ratings.getClass().getSimpleName(), ratings.getClass());
envelope.addMapping(NAMESPACE, "ArrayOfRatingHttps", RatingHttps.class);
SoapObject count = (SoapObject) SecureConnect(SOAP_ACTION[2], request, envelope);
的SecureConnect:
private Object SecureConnect(String soapAction, SoapObject request, SoapSerializationEnvelope envelope) {
try {
if(request != null) {
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
}
HttpsTransportSE androidHttpsTransport = new HttpsTransportSE(host, port, subFile, 20000);
androidHttpsTransport.call(soapAction, envelope);
return envelope.getResponse();
} catch (Exception e) {
result = e.toString();
Log.d("Tag", "Ошибка: " + e.toString());
return null;
}
}
我尝试使用Arrays,比如Rating [] ..而使用Vector,但这不起作用。 我不知道该怎么办!也许有人有一些例子怎么做?请帮忙。
答案 0 :(得分:0)
经过几个小时的互联网搜索后,我终于找到了工作实例。如果您遇到同样的问题,请确保您发送了正确的xml文件。首先,我将xml文件(我必须接收)与服务器上的xml文件进行匹配。当我发现它们是相同的时,我在标签中发现了一个错误,我将在代码中展示我是如何做到的: 我的RatingHttps类与我的问题相同; 以我的课程为例:
List<RatingHttps> rating = new ArrayList<>();
RatingHttps ratings = new RatingHttps();
ratings.CIsso = 1;
ratings.RatingIsso = 2;
ratings.RatingExt = "LOL";
ratings.RatingDate = 3;
rating.add(ratings);
接下来,请求:
request = new SoapObject(NAMESPACE, METHOD_NAME[2]);
SoapObject ratingRequest = new SoapObject(NAMESPACE, "ArrayOfRatingHttps");
request.addProperty("id", resultId.toString());
PropertyInfo pi = new PropertyInfo();
pi.setName("ratings");
pi.setValue(ratingRequest);
pi.setType("ArrayOfRatingHttps");
request.addProperty(pi);
for(int i = 0; i < rating.size(); i++) {
SoapObject rate = new SoapObject(RatingHttps.NAMESPACE, RatingHttps.class.getSimpleName());
//Добавляем значения RatingHttps в xml запрос
rate.addProperty(getPropInfo(rating.get(i).getAttributeName(0), rating.get(i), rating.get(i).getAttributeType(0)));
rate.addProperty(getPropInfo(rating.get(i).getAttributeName(1), rating.get(i), rating.get(i).getAttributeType(1)));
rate.addProperty(getPropInfo(rating.get(i).getAttributeName(2), rating.get(i), rating.get(i).getAttributeType(2)));
rate.addProperty(getPropInfo(rating.get(i).getAttributeName(3), rating.get(i), rating.get(i).getAttributeType(3)));
ratingRequest.addSoapObject(rate);
}
envelope.addMapping(NAMESPACE, RatingHttps.class.getSimpleName(), RatingHttps.class);
HttpsTransportSE androidHttpsTransport = new HttpsTransportSE(host, port, subFile, 20000);
androidHttpsTransport.call(soapAction, envelope);
我在xsd文件中找到了“ArrayOfRatingHttps”数组。如果您有wcf服务,可以在地址“https://your_address?xsd=xsd2”中找到它。我还实现了一个函数getPropInfo,这里是代码:
private PropertyInfo getPropInfo(String name, RatingHttps rating, Object type) {
PropertyInfo pi = new PropertyInfo();
pi.setName(name);
pi.setValue(rating.getCIsso());
pi.setType(type);
pi.setNamespace(RatingHttps.NAMESPACE);
return pi;
}
其中NAMESPACE我在xsd文件中找到了我的服务作为targetNamespace。所以就是这样,现在我可以发送一个复杂对象数组。希望它会帮助有人继续......