简体   繁体   English

如何使用要从Android使用的List参数创建WCF soap服务

[英]how to create WCF soap service with List param to be consume from android

I need to pass a List<string> or List<object> to consume WCF soap service from Android. 我需要传递一个List<string> or List<object>来使用Android的WCF soap服务。 I have tried a lot of ways. 我尝试了很多方法。

When i used below code: 当我使用下面的代码时:

public class MarshalArray implements Marshal{ 公共类MarshalArray实现了Marshal {

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

    return GenericType.getObject(parser.nextText());
}

@Override
public void register(SoapSerializationEnvelope envelope) {
    envelope.addMapping("http://schemas.datacontract.org/2004/07/WcfService1", "GenericType", GenericType.class, this);
}

@Override
public void writeInstance(XmlSerializer writer, Object obj)
        throws IOException {
    GenericType sp =  (GenericType) obj;

    writer.startTag("http://schemas.datacontract.org/2004/07/WcfService1", "mydata");       
    for(String str : sp.mydata){
        writer.startTag("http://schemas.datacontract.org/2004/07/WcfService1", "string");
        writer.text(str);
        writer.endTag("http://schemas.datacontract.org/2004/07/WcfService1", "string");

    }       
    writer.endTag("http://schemas.datacontract.org/2004/07/WcfService1", "mydata"); 

    }}

With below wcf service: 使用以下wcf服务:

[ServiceContract]
[ServiceKnownType(typeof(WcfService1.GenericType<string>))]
public interface IService1
{

    [OperationContract]
    [ServiceKnownType(typeof(WcfService1.GenericType<string>))]
    string GetDataList(GenericType<string> objs);
} 

 [DataContract(Name = "GenericType")]
public class GenericType<T>
{
    List<T> data;

    [DataMember]
    public List<T> mydata
    {
        get { return data; }
        set { data = value; }
    }
}

Wcf service return a soap fault error like: Internal service error.. WCF服务返回一条肥皂故障错误,例如:内部服务错误。

Then I tried in another way: 然后我尝试了另一种方式:

public class Members extends Vector<String> implements KvmSerializable {

private static final long serialVersionUID = -1166006770093411055L;

@Override
public Object getProperty(int index) {
    return this.get(index);
}

@Override
public int getPropertyCount() {
    return this.size();
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo param) {
    param.name = "String";
    param.type = PropertyInfo.STRING_CLASS;
}

@Override
public void setProperty(int arg0, Object obj) {
    this.add(obj.toString());
}

} }

With below wcf service: 使用以下wcf服务:

[ServiceContract]

public interface IService1 { 公共接口IService1 {

[OperationContract]

string GetDataList(List<string> objs);

} }

I got soap fault error which return from wcf service: 我收到了从wcf服务返回的soap错误错误:

Deserialization fail...

Every time I got an error from wcf service .. I think my wcf service got something wrong. 每次我从wcf服务收到错误时,..我认为我的wcf服务出现了问题。 If somebody know the answer .. please kindly answer my question. 如果有人知道答案..请回答我的问题。

Thanks awfully... 非常感谢...

Finally I got the answer... 终于我得到了答案...

I decided used MarshalArray class, not Members(KvmSerializable). 我决定使用MarshalArray类,而不是Member(KvmSerializable)。

While using MarshalArray, the problem is in MarshalArray class in android.. 使用MarshalArray时,问题出在Android的MarshalArray类中。

here is the old code that occur error: 这是发生错误的旧代码:

 for(String str : sp.mydata){
    writer.startTag("http://schemas.datacontract.org/2004/07/WcfService1", "string");
    writer.text(str);
    writer.endTag("http://schemas.datacontract.org/2004/07/WcfService1", "string");

}       

I changed it to like below: 我将其更改为如下所示:

for(String str:gt.mydata){
        writer.startTag("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string");
        writer.text(str);
        writer.endTag("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string");               
    }

Then it works successfully... 然后它成功工作了...

Thanks for your help, stepoverflow. 感谢您的帮助,stepoverflow。 I had to find out the answer myself.... !! 我必须自己找出答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM