2010-04-07 8 views
18

Voici mon codeComment appeler un service WCF en utilisant ksoap2 sur android?

import org.ksoap2.*; 
import org.ksoap2.serialization.*; 
import org.ksoap2.transport.*; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class ksop2test extends Activity { 
/** Called when the activity is first created. */ 


private static final String METHOD_NAME = "SayHello"; 
// private static final String METHOD_NAME = "HelloWorld"; 

private static final String NAMESPACE = "http://tempuri.org"; 
// private static final String NAMESPACE = "http://tempuri.org"; 

private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc"; 
// private static final String URL = "http://192.168.0.2:8080/webservice1/Service1.asmx"; 

final String SOAP_ACTION = "http://tempuri.org/IService1/SayHello"; 
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 
TextView tv; 
StringBuilder sb; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    tv = new TextView(this); 
    sb = new StringBuilder(); 
    call(); 
    tv.setText(sb.toString()); 
    setContentView(tv); 
} 

public void call() { 
    try { 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("name", "Qing"); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.call(SOAP_ACTION, envelope); 
    sb.append(envelope.toString() + "\n");//cannot get the xml request send 
    SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 

    //to get the data 
    String resultData = result.toString(); 
    // 0 is the first object of data 


    sb.append(resultData + "\n"); 
    } catch (Exception e) { 
    sb.append("Error:\n" + e.getMessage() + "\n"); 
    } 

} 

} 

Je peux accéder avec succès .asmx service, mais quand je tente d'appeler un service WCF la machine virtuelle dit: Erreur: attendue: END_TAG {http://schemas.xmlsoap.org/soap/envelope/} corps (position: END_TAGhttp: //schemas.xmlsoap.org/soap/envelope/} s: défaut> @ 1: 712 [email protected]

Comment imprimer ce que la demande envoyer

est ici le wcf wsdl:

<wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/"> 

<wsdl:types> 
    <xsd:schema targetNamespace="http://tempuri.org/Imports"> 
    <xsd:import schemaLocation="http://para-bj.para.local:8080/HelloWCF/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/> 
    <xsd:import schemaLocation="http://para-bj.para.local:8080/HelloWCF/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/> 
    </xsd:schema> 
</wsdl:types> 

<wsdl:message name="IService1_SayHello_InputMessage"> 
    <wsdl:part name="parameters" element="tns:SayHello"/> 
</wsdl:message> 

<wsdl:message name="IService1_SayHello_OutputMessage"> 
    <wsdl:part name="parameters" element="tns:SayHelloResponse"/> 
</wsdl:message> 

<wsdl:portType name="IService1"> 
    <wsdl:operation name="SayHello"> 
    <wsdl:input wsaw:Action="http://tempuri.org/IService1/SayHello" message="tns:IService1_SayHello_InputMessage"/> 
    <wsdl:output wsaw:Action="http://tempuri.org/IService1/SayHelloResponse" message="tns:IService1_SayHello_OutputMessage"/> 
    </wsdl:operation> 
</wsdl:portType> 

<wsdl:binding name="BasicHttpBinding_IService1" type="tns:IService1"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 

    <wsdl:operation name="SayHello"> 
    <soap:operation soapAction="http://tempuri.org/IService1/SayHello" style="document"/> 

    <wsdl:input> 
     <soap:body use="literal"/> 
    </wsdl:input> 
    <wsdl:output> 
     <soap:body use="literal"/> 
    </wsdl:output> 
    </wsdl:operation> 
</wsdl:binding> 

<wsdl:service name="Service1"> 

    <wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1"> 
    <soap:address location="http://para-bj.para.local:8080/HelloWCF/Service1.svc"/> 
    </wsdl:port> 
</wsdl:service> 

</wsdl:definitions> 

Il utilise <xsd:schema> dans la balise <wsdl:types> et asmx utilise <s:schema> dans la balise <wsdl:types> quelle est la différence?

+0

Vous ne pouvez probablement pas utiliser ksoap, car la machine virtuelle Java davalik n'est pas la même que la machine virtuelle Java Sun. Vous devrez peut-être écrire votre propre analyseur SOAP. SOAP est vraiment trop lourd pour un appareil mobile, IMO. –

+0

Mais je peux obtenir avec succès des données d'un service .asmx – Qing

Répondre

20

finalement je l'ai eu à travailler parce que l'espace de noms manqué un "/" à la fin,

Voici mon code

package cn.qing.ksop2test; 


import java.io.Writer; 

import org.ksoap2.*; 
import org.ksoap2.serialization.*; 
import org.ksoap2.transport.*; 
import org.xmlpull.v1.XmlSerializer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Xml; 
import android.widget.TextView; 

public class ksop2test extends Activity { 
/** Called when the activity is first created. */ 


private static final String METHOD_NAME = "HelloWorldRequest"; 
// private static final String METHOD_NAME = "HelloWorld"; 

private static final String NAMESPACE = "http://tempuri.org/"; 
// private static final String NAMESPACE = "http://tempuri.org"; 

private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc"; 
// private static final String URL = "http://192.168.0.2:8080/webservice1 /Service1.asmx"; 

final String SOAP_ACTION = "http://tempuri.org/IService1/HelloWorld"; 
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 
TextView tv; 
StringBuilder sb; 
private XmlSerializer writer; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    tv = new TextView(this); 
    sb = new StringBuilder(); 
    call(); 
    tv.setText(sb.toString()); 
    setContentView(tv); 
} 

public void call() { 
    try { 

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     request.addProperty("Name", "Qing"); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 


     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
     SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 

     //to get the data 
     String resultData = result.toString(); 
     // 0 is the first object of data 


     sb.append(resultData + "\n"); 
     } catch (Exception e) { 
     sb.append("Error:\n" + e.getMessage() + "\n"); 
     } 

    } 

} 
+4

Ceci est très bon code de démonstration pour accéder au service web WCF dans l'application android ..........., j'ai appliquer ce code source et résoudre mes problèmes. .... –

+0

La chose pour moi est qu'il existe un type de réponse différent en fonction de ce que la méthode renvoie. S'il y a une valeur de retour, vous utilisez un type SoapPrimitive, s'il y a un type plus complexe, vous utilisez un objet SoapObject. –

+1

vous n'avez aucune idée de combien cela m'a aidé. merci tas. J'ai également manqué ce caractère "/". :RÉ –

0

En "théorie" wcf avec la liaison HTTP de base et asmx devrait fonctionner de la même manière.

Cela peut être lié à la configuration de votre service WCF.

Nous obtenons un problème similaire si nous configurons TransferMode Streamed sur le client et Buffered sur le serveur. Bien que pas sûr si cela est pertinent dans votre cas.

1

J'utilise

private static final String SOAP_ACTION = "http://tempuri.org/IContact/GetContactCount"; 
private static final String METHOD_NAME = "GetContactCount"; 
private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL = "http://xxx.xxx.com/Contacts/ContactsService.Contacts.svc"; 

Alors peut-être que le problème est dans votre action SOAP.

L'orthographe de votre nom de méthode est correcte, c'est-à-dire AuthenticatdUser?

0

Merci Qing pour votre réponse, il est vraiment aider plein pour appeler le service WCF

Je voudrais ajouter cette rectification pour obtenir la sortie simple et complexe à partir d'un service Web après la mise en outputSoapObject dans l'enveloppe, s'il vous plaît me corriger si je suis Erreur

envelope.setOutputSoapObject(requestSoapObject); 

     // if its dotnet web service then make it true 
     if (isDotNetWebService) 
      envelope.dotNet = true; 

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(NAMESPACE + methodName, envelope); 

     if (useBodyIn) // use bodyIn if service method returns string/int 
         // etc 
     { 
      /* Gives output from webservice */ 
      responseSoapObject = (SoapObject) envelope.bodyIn; 
     } else // use getResponse() if service method returns objects or 
       // array 
     { 
      /* Gives output from webservice */ 
      responseSoapObject = (SoapObject) envelope.getResponse(); 
     } 
Questions connexes