向 WCF 服务发送数据

Posted

技术标签:

【中文标题】向 WCF 服务发送数据【英文标题】:Send Data to WCF service 【发布时间】:2014-10-17 03:22:36 【问题描述】:

我正在关注Student 课程,

public class Student implements KvmSerializable 

    private String ID;
    private String Mark;
    private String Percentage;

    public String getID() 
        return FoodItemID;
    

    public void setID(String ID) 
        ID = ID;
    

    public String getMark() 
        return Modifier;
    

    public void setMark(String mark) 
        Modifier = modifier;
    

    public String getPercentage() 
        return Quantity;
    

    public void setPercentage(String percentage) 
        Percentage = percentage;
    

    @Override
    public Object getProperty(int arg0) 
        // TODO Auto-generated method stub
        return null;
    

    @Override
    public int getPropertyCount() 
        // TODO Auto-generated method stub
        return 0;
    

    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) 
        // TODO Auto-generated method stub

    

    @Override
    public void setProperty(int arg0, Object arg1) 
        // TODO Auto-generated method stub

    


以下签名中的学生名单,

ArrayList<HashMap<String, String>> studentList;

现在我想将此列表发送到 WCF 服务,所以我创建了以下 PropertyInfo 对象,

for (int i = 0; i < studentList.size(); i++) 
    Student items = new Student();
    items.setID(studentList.get(i).get("ID"));
    items.setMark(studentList.get(i).get("Mark"));
    items.setPercentage(studentList.get(i).get("Percentage"));          
    li.add(items);

PropertyInfo pi = new PropertyInfo();
pi.setName("StudentList");
pi.setValue(li);
pi.setType(Student.class);

我的要求是

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("StudentList", pi);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
envelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
try 
    transport.call(SOAP_ACTION, envelope);
 catch (final Exception e) 
    activity.runOnUiThread(new Runnable() 

        public void run() 
            new CustomToast(activity, SOAP_ACTION + " - "
                    + e.getMessage() + " error").show();
            e.printStackTrace();
        
    );

try 
    fault = (SoapFault) envelope.bodyIn;
    activity.runOnUiThread(new Runnable() 

        public void run() 
            if (fault != null) 
                new CustomToast(activity, fault.getMessage())
                        .show();
            
        
    );
 catch (Exception e) 
    e.printStackTrace();

try 
    result = (SoapObject) envelope.bodyIn;
    System.out.println("result in getdata : " + result);
 catch (Exception e) 
    e.printStackTrace();

但这给了我以下异常,

java.lang.RuntimeException: Cannot serialize: StudentList : [com.example.citrusz.Student@410c08f8]

【问题讨论】:

什么时候你得到了异常? 在这一行transport.call(SOAP_ACTION, envelope); 【参考方案1】:

不要将PropertyInfo 传递给addProperty 函数。 而不是

request.addProperty("StudentList", pi);

直接添加

request.addProperty("StudentList", li);

【讨论】:

现在java.lang.RuntimeException: Cannot serialize: [com.example.citrusz.Student@4107f6c0]@Sam

以上是关于向 WCF 服务发送数据的主要内容,如果未能解决你的问题,请参考以下文章

从 AJAx 向 WCF Webservice 链接发送数据

从 jquery 向 wcf webservice 发送数据

使用 KSOAP2 从 Android 向 WCF 发送数据

向 WCF 发送集合时,远程服务器返回错误:NotFound

跨 WCF Web 服务请求序列化通用 XML 数据

WCF 服务 maxReceivedMessageSize basicHttpBinding 问题