IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Pb récupération résultat webservice soap (KSOAP2)


Sujet :

Android

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 6
    Points : 4
    Points
    4
    Par défaut Pb récupération résultat webservice soap (KSOAP2)
    Bonjour,

    Je suis en train de développer une application Android sur un lecteur Zebra TC20.

    J'ai besoin de récupérer les valeurs de retour d'un web service SOAP.

    Mon problème se situe au niveau de <CODSOC>, je ne récupère que le premier, je ne sais pas comment faire pour récupérer les autres valeurs. Il peut y avoir de 1 a X occurrences de <CODSOC>.

    Je suppose que je doit faire une Class de type CL_Login et l'alimenter avec la réponse mais je ne sais pas comment faire.

    Merci pour votre aide.

    Extrait Code Android
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    try {
        httpTransportSE.call(SOAP_ACTION+METHOD_NAME, envelope);
        SoapObject loresponse = (SoapObject) envelope.bodyIn;
        SoapObject logObject = (SoapObject) loresponse.getProperty("PG_Login1Result");Log.d("WS", "logObject: "+logObject);
        Log.d("WS", "Name: "+logObject.getProperty("Name"));
        int count = logObject.getPropertyCount();
        result = logObject.getProperty("Name").toString();
    }
    Réponse web service
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
    <ns1:PG_Login1Response xmlns:ns1="urn:WB_Focusmobile">
    <PG_Login1Result>
    <Status>true</Status>
    <Name>Eric DUPONT</Name>
    <BLOCHI>N</BLOCHI>
    <CODUTI>CL</CODUTI>
    <Msg>Utilisateur valide</Msg>
    <CODSOC>xxxx</CODSOC>
    <CODSOC>xyxyxyx</CODSOC>
    <CODSOC>zzzzzzzz</CODSOC>
    <CODSOC>wwwww</CODSOC>
    <CODSOC>yyyyyyy</CODSOC>
    <Resultat>1</Resultat>
    <ADMUTI>O</ADMUTI>
    </PG_Login1Result>
    </ns1:PG_Login1Response>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    Structure de la réponse
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <xsd:complexType name="tCl_Login" s0:nomcomplet="Cl_Login">
    <xsd:sequence>
    <xsd:element name="Status" type="xsd:boolean"/>
    <xsd:element name="Name" type="xsd:string"/>
    <xsd:element name="BLOCHI" type="xsd:string"/>
    <xsd:element name="CODUTI" type="xsd:string"/>
    <xsd:element name="Msg" type="xsd:string"/>
    <xsd:element name="CODSOC" type="xsd:string" maxOccurs="unbounded"/>
    <xsd:element name="Resultat" type="xsd:string"/>
    <xsd:element name="ADMUTI" type="xsd:string"/>
    </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="Cl_Login" type="s0:tCl_Login"/>

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 6
    Points : 4
    Points
    4
    Par défaut 'org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject'
    Bonjour,

    J'ai modifié mon code pour interroger mon webservice. J'ai utilisé 'wsdl2code' pour me générer la partie interrogation du webservice.

    Ce code fonctionne bien sauf sur la partie qui m’intéresse à savoir la récupération de 'CODSOC' dans la méthode 'PG_Login1'.

    J'obtiens une erreur 'org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject' -> Class tCl_Login (code en rouge)

    Je suis débutant et je ne vois pas comment modifier mon code.

    Mon code fonctionne si je commente la partie 'CODSOC' dans tCl_Login.

    Votre aide sera la bienvenue.

    CODE APPEL PG_Login1

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
     
      public void PG_Login1Async(String user,String mdp) throws Exception{
            if (this.eventHandler == null)
                throw new Exception("Async Methods Requires IWsdl2CodeEvents");
            PG_Login1Async(user, mdp, null);
        }
     
        public void PG_Login1Async(final String user,final String mdp,final List<HeaderProperty> headers) throws Exception{
     
            new AsyncTask<Void, Void, tCl_Login>(){
                @Override
                protected void onPreExecute() {
                    eventHandler.Wsdl2CodeStartedRequest();
                };
                @Override
                protected tCl_Login doInBackground(Void... params) {
                    return PG_Login1(user, mdp, headers);
                }
                @Override
                protected void onPostExecute(tCl_Login result)
                {
                    eventHandler.Wsdl2CodeEndedRequest();
                    if (result != null){
                        eventHandler.Wsdl2CodeFinished("PG_Login1", result);
                    }
                }
            }.execute();
        }
     
        public tCl_Login PG_Login1(String user,String mdp){
            return PG_Login1(user, mdp, null);
        }
     
        public tCl_Login PG_Login1(String user,String mdp,List<HeaderProperty> headers){
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.implicitTypes = true;
            soapEnvelope.dotNet = true;
            SoapObject soapReq = new SoapObject("urn:WB_Focusmobile","PG_Login1");
            soapReq.addProperty("user",user);
            soapReq.addProperty("mdp",mdp);
            soapEnvelope.setOutputSoapObject(soapReq);
            HttpTransportSE httpTransport = new HttpTransportSE(url);
            httpTransport.debug = true;
            try{
                if (headers!=null){
                    httpTransport.call("http://intranet.test.local/WB_FOCUSMOBILE_WEB/awws/PG_Login1", soapEnvelope,headers);
                }else{
                    httpTransport.call("http://intranet.test.local/WB_FOCUSMOBILE_WEB/awws/PG_Login1", soapEnvelope);
                    //System.out.println(httpTransport.requestDump);
                }
                Object retObj = soapEnvelope.bodyIn;
                //Object retObj = soapEnvelope.getResponse();
                System.out.println(retObj);
                if (retObj instanceof SoapFault){
                    SoapFault fault = (SoapFault)retObj;
                    Exception ex = new Exception(fault.faultstring);
                    if (eventHandler != null)
                        eventHandler.Wsdl2CodeFinishedWithException(ex);
                }else{
                    SoapObject result=(SoapObject)retObj;
                    if (result.getPropertyCount() > 0){
                        Object obj = result.getProperty(0);
                        SoapObject j = (SoapObject)obj;
                        tCl_Login resultVariable =  new tCl_Login (j);
                        return resultVariable;
     
                    }
                }
            }catch (Exception e) {
                if (eventHandler != null)
                    eventHandler.Wsdl2CodeFinishedWithException(e);
                e.printStackTrace();
            }
            return null;
        }
    Class tCl_Login

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    public class tCl_Login implements KvmSerializable {
        
        public boolean status;
        public String name;
        public String bLOCHI;
        public String cODUTI;
        public String msg;
        public VectorString cODSOC;
        public String resultat;
        public String aDMUTI;
        
        public tCl_Login(){}
        
        public tCl_Login(SoapObject soapObject)
        {
            if (soapObject == null)
                return;
            if (soapObject.hasProperty("Status"))
            {
                Object obj = soapObject.getProperty("Status");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    status = Boolean.parseBoolean(j.toString());
                }else if (obj!= null && obj instanceof Boolean){
                    status = (Boolean) obj;
                }
            }
            if (soapObject.hasProperty("Name"))
            {
                Object obj = soapObject.getProperty("Name");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    name = j.toString();
                }else if (obj!= null && obj instanceof String){
                    name = (String) obj;
                }
            }
            if (soapObject.hasProperty("BLOCHI"))
            {
                Object obj = soapObject.getProperty("BLOCHI");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    bLOCHI = j.toString();
                }else if (obj!= null && obj instanceof String){
                    bLOCHI = (String) obj;
                }
            }
            if (soapObject.hasProperty("CODUTI"))
            {
                Object obj = soapObject.getProperty("CODUTI");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    cODUTI = j.toString();
                }else if (obj!= null && obj instanceof String){
                    cODUTI = (String) obj;
                }
            }
            if (soapObject.hasProperty("Msg"))
            {
                Object obj = soapObject.getProperty("Msg");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    msg = j.toString();
                }else if (obj!= null && obj instanceof String){
                    msg = (String) obj;
                }
            }
    
            if (soapObject.hasProperty("CODSOC"))
            {
               SoapObject j =  (SoapObject) soapObject.getProperty("CODSOC");
               cODSOC = new VectorString(j);
            }
    
            if (soapObject.hasProperty("Resultat"))
            {
                Object obj = soapObject.getProperty("Resultat");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    resultat = j.toString();
                }else if (obj!= null && obj instanceof String){
                    resultat = (String) obj;
                }
            }
            if (soapObject.hasProperty("ADMUTI"))
            {
                Object obj = soapObject.getProperty("ADMUTI");
                if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
                    SoapPrimitive j =(SoapPrimitive) obj;
                    aDMUTI = j.toString();
                }else if (obj!= null && obj instanceof String){
                    aDMUTI = (String) obj;
                }
            }
        }
        @Override
        public Object getProperty(int arg0) {
            switch(arg0){
                case 0:
                    return status;
                case 1:
                    return name;
                case 2:
                    return bLOCHI;
                case 3:
                    return cODUTI;
                case 4:
                    return msg;
                case 5:
                    return cODSOC;
                case 6:
                    return resultat;
                case 7:
                    return aDMUTI;
            }
            return null;
        }
        
        @Override
        public int getPropertyCount() {
            return 8;
        }
        
        @Override
        public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
            switch(index){
                case 0:
                    info.type = PropertyInfo.BOOLEAN_CLASS;
                    info.name = "Status";
                    break;
                case 1:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "Name";
                    break;
                case 2:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "BLOCHI";
                    break;
                case 3:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "CODUTI";
                    break;
                case 4:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "Msg";
                    break;
                case 5:
                    info.type = PropertyInfo.VECTOR_CLASS;
                    info.name = "CODSOC";
                    break;
                case 6:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "Resultat";
                    break;
                case 7:
                    info.type = PropertyInfo.STRING_CLASS;
                    info.name = "ADMUTI";
                    break;
            }
        }
        
    
        public String getInnerText() {
            return null;
        }
        
        
    
        public void setInnerText(String s) {
        }
        
        
        @Override
        public void setProperty(int arg0, Object arg1) {
        }
        
    }
    Class VectorString

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    public class VectorString extends Vector<String> implements KvmSerializable {
     
     
        public VectorString(){}
     
        public VectorString(SoapObject soapObject)
        {
            if (soapObject == null)
                return;
            if (soapObject != null){
                int size = soapObject.getPropertyCount();
                for (int i0=0;i0<size;i0++){
                    Object obj = soapObject.getProperty(i0);
                    if (obj !=null && obj.getClass().equals(SoapPrimitive.class)){
                        SoapPrimitive j0 =(SoapPrimitive) soapObject.getProperty(i0);
                        String j1=j0.toString();
                        add(j1);
                    }
                }
            }
        }
        @Override
        public Object getProperty(int arg0) {
            return this.get(arg0);
        }
     
        @Override
        public int getPropertyCount() {
            return this.size();
        }
     
        @Override
        public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
            info.name = "String";
            info.type = String.class;
        }
     
     
     
        public String getInnerText() {
            return null;
        }
     
     
     
     
        public void setInnerText(String s) {
        }
     
     
        @Override
        public void setProperty(int arg0, Object arg1) {
        }
     
    }

    Traitement de la réponse du webservice

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    @Override
    public void Wsdl2CodeFinished(String methodName, Object Data) {
        switch (methodName){
            case "PG_Test":
                break;
            case "PG_Login1":
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Resultat");
                tCl_Login User = (tCl_Login) Data;
                builder.setMessage("Le resultat est: "+User.name);
                builder.show();
                break;
            default:
                break;
        }
    
    }

  3. #3
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Totalement au débotté, remplace ta ligne en rouge par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SoapPrimitive j =  (SoapPrimitive) soapObject.getProperty("CODSOC");
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    J'ai déjà testé mais alors, c'est la ligne 'cODSOC = VectorString(j) qui ne fonctionne plus car la class VectorString attends un SoapObject.

    Je suis en train de voir pour remplacer VectorString par un Vector<String>.

    Si j'alimente la la mano, ça fonctionne mais maintenant, il faut que je l'alimente en automatique.

    J'ai commencé le code ci-dessous mais je bloque.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
            if (soapObject.hasProperty("CODSOC"))
            {
                //SoapObject j =  (SoapObject) soapObject.getProperty("CODSOC");
                //cODSOC = new VectorString(j);
    
                cODSOC = new Vector();
                Integer i = (Integer) soapObject.getPropertyCount();
                for (int j=0;j<i;j++){
                     Object obj = soapObject.getProperty(j);
                    //si obj = "CODSOC" alors ajouter valeur à cODSOC
    
                };
    
    
                // Alimentation à la mano
                //cODSOC.addElement("ARIS");
                //cODSOC.addElement("BPLUS");
                //cODSOC.addElement("CMNEUR");
                //cODSOC.addElement("MORILLON");
    
            }

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Bon, au final j'ai modifié mon code comme suit et ça fonctionne.

    Après je sais pas si c'est le plus propre mais ça fonctionne!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
            if (soapObject.hasProperty("CODSOC"))
            {
                cODSOC = new Vector();
                Integer i = (Integer) soapObject.getPropertyCount();
                for (int j=0;j<i;j++){
                    Object obj = soapObject.getProperty(j);
                    PropertyInfo pi = new PropertyInfo();
                    soapObject.getPropertyInfo(j,pi);
                    String name = pi.getName();
                    System.out.println(name);
                    if (name.equals("CODSOC")){
                        cODSOC.addElement(obj.toString());
                    };
                };
            }

Discussions similaires

  1. [WD18] WebService / SOAP - Problème de récupération des données
    Par Zwenthys dans le forum WinDev
    Réponses: 0
    Dernier message: 15/07/2014, 10h35
  2. [PEAR] Récupération résultat d'une requête
    Par lodan dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 01/07/2006, 15h54
  3. Récupération résultat d'une PS dans une variable
    Par Goldorak2 dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 06/06/2006, 13h57
  4. Webservice SOAP et fichier attaché ?
    Par Bruno13 dans le forum Services Web
    Réponses: 13
    Dernier message: 03/04/2006, 17h36
  5. Réponses: 5
    Dernier message: 13/03/2006, 18h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo