Manipulation du reponse SOAP avec android
Bonjour,
je suis entrain de develloper une application android qui fait appel à une methode disponible via un web service en utilisant le protocole SOAP .
la reponse SOAP présente plusieurs objets et chaque objet contient plusieurs attributs comme ça
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
| <?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:trouverAvecDateMResponse xmlns:ns2="http://medecin/">
<return>
<bureau>7</bureau>
<idMedecin>24</idMedecin>
<motDePasse>aaaa</motDePasse>
<nom>benslimen</nom>
<prenom>ahmed</prenom>
<service>aaaaa</service>
<specialite>esthétique</specialite>
</return>
<return>
<bureau>7</bureau>
<idMedecin>24</idMedecin>
<motDePasse>aaaa</motDePasse>
<nom>benslimen</nom>
<prenom>ahmed</prenom>
<service>aaaaa</service>
<specialite>esthétique</specialite>
</return>
<return>
<bureau>7</bureau>
<idMedecin>51</idMedecin>
<motDePasse>aaaa</motDePasse>
<nom>mourad</nom>
<prenom>dhib</prenom>
<service>odhra</service>
<specialite>esthétique</specialite>
</return>
</ns2:trouverAvecDateMResponse>
</S:Body>
</S:Envelope> |
mon probléme : j'ai pas trouvé comment recuperer chaque attribut de chaque medecin
et voici le code java que j'ai utilisé:
JavaWSActivity.java
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
| package com.android;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class JavaWSActivity extends Activity {
Button b;
TextView tv;
EditText et;
EditText et1;
ProgressBar pg;
String arg0;
String arg1;
String displayText;
Object[] retourTab = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Name Text control
et = (EditText) findViewById(R.id.editText1);
et1=(EditText) findViewById(R.id.editText2);
//Display Text control
tv = (TextView) findViewById(R.id.tv_result);
//Button to trigger web service invocation
b = (Button) findViewById(R.id.button1);
//Display progress bar until web service invocation completes
pg = (ProgressBar) findViewById(R.id.progressBar1);
//Button Click Listener
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Check if Name text control is not empty
if (et.getText().length() != 0 && et.getText().toString() != "") {
//Get the text control value
arg0 = et.getText().toString();
arg1 = et1.getText().toString();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
} else {
tv.setText("Please enter name");
}
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
retourTab =(Object[]) WebService.prendreRdvWS(arg0,arg1,"trouverAvecDateM");
return null;
}
@Override
protected void onPostExecute(Void result) {
tv.setText(retourTab[2].toString());
pg.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
pg.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
} |
et WebService.java
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
| package com.android;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebService {
//Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://medecin/";
//Webservice URL - WSDL File location
private static String URL = "http://10.0.2.2:8080/WebApplication5/medecinsws?WSDL";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://medecin/";
public static Object[] retourTab = null;
public static Object[] prendreRdvWS(String arg0, String arg1,String webMethName) {
String resTxt = null;
// Create request
SoapObject request = new SoapObject(NAMESPACE, webMethName);
// Add the property to request object*/
request.addProperty("jour",arg0);
request.addProperty("specialite",arg1);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
// Get the response
SoapObject response = (SoapObject) envelope.bodyIn;
int nbr=0;
if(response != null){
nbr = response.getPropertyCount();
retourTab = new Object[nbr];
for(int i=0; i<nbr; i++){
retourTab[i] = response.getProperty(i);
}
}
} catch (Exception e) {
//Print error
System.out.println(e.getMessage());
}
return retourTab;
}
} |