【问题标题】:How to call a WCF service using ksoap2 on android?如何在 android 上使用 ksoap2 调用 WCF 服务?
【发布时间】:2010-04-07 01:39:10
【问题描述】:

这是我的代码

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ksop2test extends Activity {
 /** Called when the activity is first created. */


 private static final String METHOD_NAME = "SayHello";
// private static final String METHOD_NAME = "HelloWorld";

 private static final String NAMESPACE = "http://tempuri.org";
// private static final String NAMESPACE = "http://tempuri.org";

 private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
// private static final String URL = "http://192.168.0.2:8080/webservice1/Service1.asmx";

 final String SOAP_ACTION = "http://tempuri.org/IService1/SayHello";
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
 TextView tv;
 StringBuilder sb;



 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  tv = new TextView(this);
  sb = new StringBuilder();
  call();
  tv.setText(sb.toString());
  setContentView(tv);
 }

 public void call() {
  try {

   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

   request.addProperty("name", "Qing");

   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
     SoapEnvelope.VER11);
   envelope.dotNet = true;
   envelope.setOutputSoapObject(request);


   HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
   androidHttpTransport.call(SOAP_ACTION, envelope);
   sb.append(envelope.toString() + "\n");//cannot get the xml request send
   SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

   //to get the data
   String resultData = result.toString();
   // 0 is the first object of data 


   sb.append(resultData + "\n");
  } catch (Exception e) {
   sb.append("Error:\n" + e.getMessage() + "\n");
  }

 }

}

我可以成功访问 .asmx 服务,但是当我尝试调用 wcf 服务时 虚拟机说道: 错误: 预期:END_TAG{http://schemas.xmlsoap.org/soap/envelope/}正文(位置:END_TAGhttp://schemas.xmlsoap.org/soap/envelope/}s:Fault>@1:712 in java.io.InputStreamReader@43ba6798

如何打印请求发送的内容?

这是 wcf wsdl:

<wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/">

<wsdl:types>
  <xsd:schema targetNamespace="http://tempuri.org/Imports">
  <xsd:import schemaLocation="http://para-bj.para.local:8080/HelloWCF/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
  <xsd:import schemaLocation="http://para-bj.para.local:8080/HelloWCF/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
  </xsd:schema>
</wsdl:types>

<wsdl:message name="IService1_SayHello_InputMessage">
  <wsdl:part name="parameters" element="tns:SayHello"/>
</wsdl:message>

<wsdl:message name="IService1_SayHello_OutputMessage">
  <wsdl:part name="parameters" element="tns:SayHelloResponse"/>
</wsdl:message>

<wsdl:portType name="IService1">
  <wsdl:operation name="SayHello">
    <wsdl:input wsaw:Action="http://tempuri.org/IService1/SayHello" message="tns:IService1_SayHello_InputMessage"/>
    <wsdl:output wsaw:Action="http://tempuri.org/IService1/SayHelloResponse" message="tns:IService1_SayHello_OutputMessage"/>
  </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="BasicHttpBinding_IService1" type="tns:IService1">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>

  <wsdl:operation name="SayHello">
   <soap:operation soapAction="http://tempuri.org/IService1/SayHello" style="document"/>

     <wsdl:input>
       <soap:body use="literal"/>
     </wsdl:input> 
     <wsdl:output>
       <soap:body use="literal"/>
     </wsdl:output>
  </wsdl:operation>
</wsdl:binding>

<wsdl:service name="Service1">

  <wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1">
    <soap:address location="http://para-bj.para.local:8080/HelloWCF/Service1.svc"/>
  </wsdl:port>
</wsdl:service>

</wsdl:definitions>

它在标签&lt;wsdl:types&gt;中使用&lt;xsd:schema&gt; 并且 asmx 在标签 &lt;wsdl:types&gt; 中使用 &lt;s:schema&gt; 有什么区别?

【问题讨论】:

  • 您可能无法使用 ksoap,因为 davalik JVM 与 Sun JVM 不同。您可能需要编写自己的 SOAP 解析器。 SOAP 对于移动设备来说实在是太重了,IMO。
  • 但是我可以成功地从 .asmx 服务获取数据

标签: android wcf ksoap2


【解决方案1】:

我终于让它工作了 因为命名空间最后漏掉了一个“/”,

以下是我的代码

package cn.qing.ksop2test;


import java.io.Writer;

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.TextView;

public class ksop2test extends Activity {
/** Called when the activity is first created. */


private static final String METHOD_NAME = "HelloWorldRequest";
//  private static final String METHOD_NAME = "HelloWorld";

private static final String NAMESPACE = "http://tempuri.org/";
//  private static final String NAMESPACE = "http://tempuri.org";

private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
//  private static final String URL = "http://192.168.0.2:8080/webservice1  /Service1.asmx";

final String SOAP_ACTION = "http://tempuri.org/IService1/HelloWorld";
//  final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
private XmlSerializer writer;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tv = new TextView(this);
    sb = new StringBuilder();
    call();
    tv.setText(sb.toString());
    setContentView(tv);
}

public void call() {
    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("Name", "Qing");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);


        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        //to get the data
        String resultData = result.toString();
        // 0 is the first object of data 


        sb.append(resultData + "\n");
        } catch (Exception e) {
        sb.append("Error:\n" + e.getMessage() + "\n");
        }

    }

}

【讨论】:

  • 这是一个非常好的演示代码,用于在 android 应用程序中访问 WCF Web 服务............,我已经应用了这个源代码并解决了我的问题......
  • 对我来说,有不同的响应类型取决于方法返回的内容。如果有一个返回值,则使用 SoapPrimitive 类型,如果有更复杂的类型,则使用 SoapObject。
  • 你不知道这对我有多大帮助。多谢了。我也错过了那个“/”字符。 :D
  • 你好。我很久以前就用过你的代码。今天我的应用程序已被 Google Play 禁止,因为它使用的是“tempuri.org”。你知道为什么需要 tempuri.org 吗?我正在尝试将其从您的代码中删除,但随后它停止工作。请问有什么办法吗?
【解决方案2】:

我正在使用

private static final String SOAP_ACTION = "http://tempuri.org/IContact/GetContactCount";
private static final String METHOD_NAME = "GetContactCount";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://xxx.xxx.com/Contacts/ContactsService.Contacts.svc";

所以问题可能出在您的 SOAP 操作中。

您的方法名称的拼写是否正确,即 AuthenticadUser?

【讨论】:

    【解决方案3】:

    在“理论上”wcf 与基本的 http 绑定和 asmx 应该工作相同。

    这可能与您的 WCF 服务的配置方式有关。

    如果我们在客户端配置 TransferMode Streamed 并在服务器上配置 Buffered,我们会遇到类似的问题。虽然不确定这是否与您的情况相关。

    【讨论】:

      【解决方案4】:

      感谢Qing的回答,调用WCF服务真的很有帮助

      在将 outputSoapObject 设置为信封后,我想添加此更正以从 Web 服务获取简单和复杂的输出,如果我错了,请纠正我

      envelope.setOutputSoapObject(requestSoapObject);
      
              // if its dotnet web service then make it true
              if (isDotNetWebService)
                  envelope.dotNet = true;
      
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
              androidHttpTransport.call(NAMESPACE + methodName, envelope);
      
              if (useBodyIn) // use bodyIn if service method returns string/int
                              // etc
              {
                  /* Gives output from webservice */
                  responseSoapObject = (SoapObject) envelope.bodyIn;
              } else // use getResponse() if service method returns objects or
                      // array
              {
                  /* Gives output from webservice */
                  responseSoapObject = (SoapObject) envelope.getResponse();
              } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-13
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多