Présentation
Présentation
Dans la continuité de la série article du protocole SOAP , nous allons voir comment fait un client sur android avec la bibliothèque : ksoap2-android, qui va nous faciliter le travail. ksoap2 va nous permettre de créer l'ensemble des éléments du protocole HTTP et SOAP. Par la suite, nous saurons qu'à configurer cet élément pour accéder au service SOAP.
Prérequis
Comme dit précédemment, il va nous falloir la bibliothèque ksoap2-android et aussi l'implémenter dans votre projet. Mais avant tout, il vous faudra télécharger la bibliothèque. si vous voulait aussi accédé au site de la bibliothèque
Pendant que ca se télécharger avec ta ADSL du future, nous allons créer un paquet “soap”ou on va ranger votre class qui cera apple “ClientSOAP.java”
Au monument ou vous avez tout fini, changez le mode d'affichage de votre projet de “Android” à "Projet". Cela va nous permettre de voir le dossier “lib” ou sera notre bibliothèque. Faite glisser le fichier dans ce dossier.
Normalement un pop-up va vous demander si vous voulez intégrer la bibliothèque dans le projet. Si vous acceptez , il va implémenter sans que vous le faites vous meme. Sinon il vous faudra rajouter cette ligne :
// build.gradle(Module:[nom du projet])
dependencies {
...
implementation files('libs\\ksoap2-android-assembly-[Version]-jar-with-dependencies.jar')
...
}
// ...soap.SoapClient.java
import android.content.Context;
import android.util.Log;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class SoapClient {
// thread qui fera les envoie SOAP sans bloque
private Thread thread = new Thread() ;
// Definition du service
private String NameSpace ;
private String URL ;
// recuperation du context de l'ojet
private Context context ;
public void setCookie(String cookie) {
this.cookie = cookie;
}
// cookie
private String cookie = null ;
// list
private ArrayList<SoapRequest> soapRequestArrayList = new ArrayList<SoapRequest>();
private ArrayList<SoapReponse> soapReponseArrayList = new ArrayList<SoapReponse>();
// Objet lock
private final Object Lock = new Object() ;
public SoapClient(Context context, String nameSpace, String URL) {
this.NameSpace = nameSpace;
this.URL = URL;
this.context = context;
}
public void exe (SoapRequest request) {
synchronized(Lock) {
soapRequestArrayList.add(request);
Log.i("SOAP","Resquest is add to list of treatment");
}
if (!thread.isAlive() ) {
thread = new Thread( new soapExe( this));
thread.start();
Log.i("SOAP","Soap Thread is start ");
}
}
public SoapReponse getSoapReponse () throws InterruptedException {
// is block main thread
while (true) {
synchronized(Lock) {
if (soapReponseArrayList.() != 0) {
SoapReponse tamp = soapReponseArrayList.remove(0);
Log.i("SOAP", "Getting reponse of " + tamp.getMethode());
return tamp;
}
}
Thread.sleep(10);
}
}
private static class soapExe implements Runnable {
private SoapClient context ;
public soapExe(SoapClient context) {
this.context = context;
}
@Override
public void run() {
while (true) {
// Recuperation de la request
SoapRequest request = null ;
String NameSpace = null ;
String URL = null ;
String cookie = null ;
synchronized(this.context.Lock) {
// arrest thread si il y a plus de request
if (this.context.soapRequestArrayList.() <= 0) {
break;
}
request = context.soapRequestArrayList.remove(0);
NameSpace = this.context.NameSpace ;
URL = this.context.URL ;
cookie = this.context.cookie ;
Log.i("SOAP", "Start treatment of the request : "+ request.getMethode());
Log.i("SOAP", "NameSpace : "+ NameSpace);
Log.i("SOAP", "URL : "+ URL);
Log.i("SOAP", "cookie : "+ cookie);
}
try {
// Creation d'un objet SOAP
SoapObject soapObject = new SoapObject(NameSpace ,request.getMethode());
// insertion des proprieties dans le soapObject
for (int i = 0 ; i < request.getArray().() ; i++ ) {
soapObject.addProperty(
request.getArray().get(i).getKey(),
request.getArray().get(i).getValue()
);
}
// Creation de l'envelope SOAP
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
// Creation du transporter HTTP
HttpTransportSE http = new HttpTransportSE(URL);
List headers = null;
if (cookie != null) {
// Envoi avec cookie et recuperation du header
List header = new ArrayList<HeaderProperty>() ;
header.add( new HeaderProperty("Cookie",cookie));
List headerList = http.call(NameSpace + "#" + request.getMethode(),envelope , header);
headers = headerList ;
}
else {
// Envoi et recuperation du header
List headerList = http.call(NameSpace+ "#" + request.getMethode(),envelope , null);
headers = headerList ;
}
// Display header HTTP
String getcookie = null ;
if (request.isGetHTTPCookie()){
for (Object header : headers) {
HeaderProperty headerProperty = (HeaderProperty) header;
String headerKey = headerProperty.getKey();
String headerValue = headerProperty.getValue();
if (Objects.equals(headerKey, "Set-Cookie")) {
getcookie = headerValue ;
}
Log.i("SOAP",headerKey +" : " + headerValue);
}
}
Log.i("SOAP",envelope.getResponse().toString());
synchronized(this.context.Lock) {
this.context.soapReponseArrayList.add(new SoapReponse(envelope.getResponse().toString(), request.getMethode(), getcookie)) ;
}
} catch (IOException | XmlPullParserException e) {
synchronized(this.context.Lock) {
this.context.soapReponseArrayList.add(new SoapReponse("{ Result:-1000 }")) ;
}
Log.i("SOAP","Error : "+e.getMessage()+" | " + e.getClass().toString());
}
}
Log.i("SOAP", "Thread is end ");
}
}
}
// ...soap.SoapRequest.java
import java.util.ArrayList;
public class SoapRequest {
private String methode = "" ;
public boolean isGetHTTPCookie() {
return isGetHTTPCookie;
}
public void setGetHTTPCookie(boolean getHTTPCookie) {
isGetHTTPCookie = getHTTPCookie;
}
private boolean isGetHTTPCookie = false ;
public ArrayList<SoapProperty> getArray() {
return Array;
}
private ArrayList<SoapProperty> Array = new ArrayList<SoapProperty>();
public SoapRequest(String methode) {
this.methode = methode;
}
public SoapRequest(String methode, boolean GetHTTPCookie) {
this.methode = methode;
this.isGetHTTPCookie = GetHTTPCookie;
}
public SoapRequest(String methode, ArrayList<SoapProperty> array ) {
this.methode = methode;
this.Array = array;
}
public SoapRequest(String methode, ArrayList<SoapProperty> array , boolean GetHTTPCookie ) {
this.methode = methode;
this.isGetHTTPCookie = GetHTTPCookie;
this.Array = array;
}
public String getMethode() {
return methode;
}
public void setMethode(String methode) {
this.methode = methode;
}
}
// ...soap.SoapProperty.java
public class SoapProperty {
private String key = "";
private String value = "";
public SoapProperty(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
// ...soap.SoapReponse.java
public class SoapReponse {
private String cookie ;
private String data ;
private String Methode ;
public SoapReponse(String data) {
this.data = data;
}
public SoapReponse(String data, String methode) {
this.data = data;
this.Methode = methode;
}
public SoapReponse(String data, String methode , String cookie) {
this.data = data;
this.Methode = methode;
this.cookie = cookie;
}
public String getMethode() {
return Methode;
}
public String getCookie() {
return cookie;
}
public String getData() {
return data;
}
}