使用 KSOAP2 从 Android 向 WCF 发送数据

Posted

技术标签:

【中文标题】使用 KSOAP2 从 Android 向 WCF 发送数据【英文标题】:Send data to WCF from Android using KSOAP2 【发布时间】:2014-10-12 04:30:55 【问题描述】:

我的代码是,

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("ItemList", mainObject.toString());
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;
 catch (Exception e) 
    e.printStackTrace();

而 mainObject 是一个JSONObject,其中包含以下数据,

"ItemList":["ID":"","Name":"Abc","Mark":"81","ID":"","Name":"XYZ","Mark":"82"]

我通过以下方式在我的 WCF 中收到此信息。

[OperationContract]
void InsertUpdateEntry(Items ItemList);

Items 类是

[CollectionDataContract(Namespace = "")]
public class Items : List<clsitems>


clsitems 类是

[DataContract]
public class clsitems

    [DataMember]
    public string ID  get; set; 

    [DataMember]
    public string Name  get; set; 

    [DataMember]
    public string Mark  get; set; 

最后我面临以下异常。

java.io.IOException: HTTP request failed, HTTP status: 500

我只想将数据发送到Items 类,因此任何其他解决方案也是可以接受的。

【问题讨论】:

【参考方案1】:

尝试在 CollectionDataContract 属性上设置 ItemName。例如:

[CollectionDataContract(Name = "Custom0List", ItemName = "CustomItem")]
public class Items : List<clsitems>


CollectionDataContractAttribute 旨在简化处理来自非提供者的数据时的互操作性,并控制序列化实例的确切形状。为此,ItemName 属性使您能够控制集合中重复项的名称。这在提供者不使用 XML 元素类型名称作为数组项名称时特别有用,例如,如果提供者使用“String”作为元素类型名称而不是 XSD 类型名称“字符串”。

取自here

【讨论】:

【参考方案2】:

找到您问题的解决方案.. HTTP 500 是内部服务器错误,因此您应该检查服务器是否使用 SOAPUI 或其他一些初学者工具。然后确保您可以从设备访问 URL(在这种情况下为 IP 号),然后开始调试 ksaop 调用.. 确保您的 web 服务工作正常

【讨论】:

我可以发送简单的数据,现在我想发送复杂的数据,服务器工作正常,这里的错误是数据类型不匹配,问题是我不知道如何为@987654322发送数据@WCF 中的类。 项目是模型类? 使用此链接***.com/questions/19159629/…【参考方案3】:

您可以像这样在服务器端启用跟踪并检查生成的文件,包括记录的 SOAP 消息。然后你就可以识别问题了。将此添加到您的 app.config 或 web.config。 (您可能需要更改一些设置

<system.diagnostics>
<sources>
  <source propagateActivity="true" name="System.ServiceModel" switchValue="All">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml">
        <filter type="" />
      </add>
    </listeners>
  </source>

  <source name="System.ServiceModel.MessageLogging">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml" />
    </listeners>
  </source>

  <source name="CardSpace">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.IO.Log">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.Runtime.Serialization">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.IdentityModel">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add name="xml">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="Traces.svclog" type="System.Diagnostics.XmlWriterTraceListener"
    name="xml" traceOutputOptions="ProcessId, ThreadId">
    <filter type="" />
  </add>
</sharedListeners>
</system.diagnostics>

【讨论】:

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

使用 ksoap2 使用 WCF 服务

使用具有复杂对象的 KSoap2 调用 WCF 服务。 WCF 接收空值

Ksoap2 Android - 如何为复杂对象的子属性指定命名空间?

在Android 中使用KSOAP2调用WebService(转)

Android 使用KSOAP2调用WebService

android使用Ksoap2访问WebService时,是不是可以设置超时(Timeout)?