我尝试调用Web服务时出现KSoap错误消息

时间:2012-10-11 08:17:30

标签: c# android .net web-services

我正在使用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>

有人可以给我一个关于此错误消息的提示吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

SOAP_ACTION NAMESPACE + METHOD

所以,我认为你在调用自己的服务时出错了。

假设您已经更正了方法和命名空间,则以下内容应该有效。

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