在android中访问C#.net web服务

时间:2010-10-08 06:35:23

标签: c# android web-services

如何使用Android?

使用.net网络服务

我的代码是这样的......

package Webservices.pck;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;

public class Webservices extends Activity 
{  
 private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
 private static final String METHOD_NAME = "HelloWorld";
 private static final String NAMESPACE = "http://tempuri.org/";
 private static final String URL = "http://ipaddress/Service1.asmx";
 //private Object resultRequestSOAP = null;
    @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       try
       {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request); 

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.debug = true;
            envelope.dotNet = true;
            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapObject resultRequestSOAP =  (SoapObject)envelope.bodyIn;
            String result = (String)resultRequestSOAP.getProperty(0).toString();
            tv.setText(result);
            this.setContentView(tv);
       }
       catch (Exception aE)
       {
            tv.setText(aE.toString());
            this.setContentView(tv);
       }
    }
}

在我正在使用的代码中。

String URL = "http://ipaddress/Service1.asmx"; 

然后错误: - org.xmlpull.v1.xmlPullParserException: expected:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope(position:START_TAG<html>@1:6 in java.io.InputStreamReader@4375fda8)

1 个答案:

答案 0 :(得分:1)

您正在访问html页面而不是SOAP服务。解析器Exception已经告诉你出了什么问题。

你有这样的数据

<html><body>... </body></html>

页面应返回类似

的内容
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <YourFunctionToCall... />
    </soapenv:Body>
</soapenv:Envelope>

也许您的网址中有拼写错误,或某种身份验证或其他类型的错误,因此它返回了HTML错误而不是肥皂请求/响应。