【问题标题】:KSoap Error message when I try to call to the webservice当我尝试调用 Web 服务时出现 KSoap 错误消息
【发布时间】:2012-10-11 08:17:30
【问题描述】:

我正在使用 SOAP 与用.Net 编写的 Web 服务进行通信。源码如下:

public class SlipperyAsSoap extends Activity {

Button sendMsg;
TextView resultText;
EditText query;

final String NAMESPACE = "http://tempuri.org/";
final String SOAP_ACTION = "http://tempuri.org/IIOService/SendMessage";
final String METHOD_NAME = "SupportedMessageTypes";
final String URL = "http://biovnap.no.netserver/Bio/IOService.svc?wsdl";

// private static String SOAP_ACTION1 =
// "http://tempuri.org/FahrenheitToCelsius";
// private static String SOAP_ACTION2 =
// "http://tempuri.org/CelsiusToFahrenheit";
// private static String NAMESPACE = "http://tempuri.org/";
// private static String METHOD_NAME1 = "FahrenheitToCelsius";
// private static String METHOD_NAME2 = "CelsiusToFahrenheit";
// private static String URL =
// "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sendMsg = (Button) findViewById(R.id.button1);
    resultText = (TextView) findViewById(R.id.textView1);
    query = (EditText) findViewById(R.id.editText1);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    sendMsg.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            // Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            // Use this to add parameters
            // request.addProperty("Fahrenheit",
            // query.getText().toString());

            // Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(
                        URL);
                androidHttpTransport.debug = true;
                // this is the actual part that will call the webservice

                androidHttpTransport.call(SOAP_ACTION, envelope);

                // Get the SoapResult from the envelope body.
                SoapObject result = (SoapObject) envelope.bodyIn;

                if (result != null) {
                    // Get the first property and change the label text
                    resultText.setText(result.getProperty(0).toString());
                } else {
                    Toast.makeText(getApplicationContext(), "No Response",
                            Toast.LENGTH_LONG).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                Log.e("XMLPullParser: ", e.getMessage());
            }
        }

    });
}
}

如您所见,我尝试联系 W3Schools 的网络服务,只是为了测试。这很好用,但是当我尝试联系我的网络服务时,我的网络服务可以与我的手机在同一个网络中访问,我只从 LogCat 收到这个错误:

10-11 10:11:21.435: E/XMLPullParser:(1043): unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@41a790d8) 

我尝试达到的方法是这样的:

wsdl:operation name="SupportedMessageTypes">
<wsdl:input wsaw:Action="http://tempuri.org/IIOService/SupportedMessageTypes" message="tns:IIOService_SupportedMessageTypes_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IIOService/SupportedMessageTypesResponse" message="tns:IIOService_SupportedMessageTypes_OutputMessage"/>
</wsdl:operation>

当我尝试执行androidHttpTransport.call(SOAP_ACTION, envelope); 时会发生此错误,我知道这应该放在 AsyncTask 中,每个网络操作都应该以这种方式构造,所以请不要抱怨。

这是来自 WCF 测试客户端的请求 XML

 <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/IIOService/SupportedMessageTypes</a:Action>
    <a:MessageID>urn:uuid:5c3f721a-f0d6-4be9-9699-10b48a2ee146</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
  </s:Header>
  <s:Body>
    <SupportedMessageTypes xmlns="http://tempuri.org/" />
  </s:Body>
</s:Envelope>

和响应 XML

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/IIOService/SupportedMessageTypesResponse</a:Action>
    <a:RelatesTo>urn:uuid:85436e79-86bb-4370-843c-b1047975b864</a:RelatesTo>
  </s:Header>
  <s:Body>
    <SupportedMessageTypesResponse xmlns="http://tempuri.org/">
      <SupportedMessageTypesResult xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <b:string>EurodacTestReplyGenerator</b:string>
        <b:string>EurodacBlock</b:string>
      </SupportedMessageTypesResult>
    </SupportedMessageTypesResponse>
  </s:Body>
</s:Envelope>

谁能给我关于这个错误信息的提示?

提前致谢!

【问题讨论】:

    标签: c# android .net web-services


    【解决方案1】:

    SOAP_ACTIONNAMESPACE + METHOD

    所以,这就是我认为您在调用自己的服务时出错的地方。

    假设您已更正分配的方法和命名空间,以下应该可以工作。

    final String NAMESPACE = "http://tempuri.org/";
    final String SOAP_ACTION = "http://tempuri.org/IIOService/SupportedMessageTypes";
    final String METHOD_NAME = "SupportedMessageTypes";
    

    【讨论】:

    • 感谢您的回答,但我不断收到问题中所述的错误消息。在 XML 标头中,命名空间以这种方式声明:targetNamespace="http://tempuri.org/"&gt;SupportedMessageTypes 以这种方式声明:&lt;wsdl:operation name="SupportedMessageTypes"&gt; &lt;soap12:operation soapAction="http://tempuri.org/IIOService/SupportedMessageTypes" style="document"/&gt; &lt;wsdl:input&gt; &lt;soap12:body use="literal"/&gt; &lt;/wsdl:input&gt; &lt;wsdl:output&gt; &lt;soap12:body use="literal"/&gt; &lt;/wsdl:output&gt; &lt;/wsdl:operation&gt;
    • 我不确定,但也许您可以在我的回答中尝试更新后的 SOAP_ACTION。实际上我自己还没有处理过嵌套目录。
    • 很抱歉,请再试一次method = IIOService/SupportedMessageTypes
    • 将命名空间和操作保留为我现在的答案中的内容。只需替换方法即可。
    • 也不走运。这太令人不安了。
    猜你喜欢
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多