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:
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:
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:
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"/> |
'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:
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:
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:
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:
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;
}
} |