简体   繁体   中英

Access C# .net web service in android

How can I use .net web services using android?

My code is like this...

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);
       }
    }
}

In this code I am using.

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

then error :-- 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)

You are accessing an html page and not a SOAP service. The parser Exception has already told you what's wrong.

You got data like this

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

while the page should return something like

<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>

Maybe you have a typo in your URL, or some kind of Authentication or some other kind of error, so that it returned an HTML error instead of the Soap Request/Response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM