2010-10-08 6 views
1

Comment puis-je utiliser les services Web .net à l'aide d'android?Accéder au service Web C# .net dans android

Mon code est comme ça ...

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

Dans ce code, je me sers.

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

puis erreur: - org.xmlpull.v1.xmlPullParserException: expected:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope(position:START_TAG<html>@1:6 in [email protected])

Répondre

1

Vous accédez à une page html et non un service SOAP. L'exception de l'analyseur vous a déjà dit ce qui ne va pas.

Vous avez obtenu des données comme celui-ci

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

alors que la page doit retourner quelque chose comme

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

Peut-être que vous avez une faute de frappe dans votre URL, ou une sorte d'authentification ou d'un autre type d'erreur , de sorte qu'il renvoyait une erreur HTML au lieu de la demande/réponse de savon.

Questions connexes