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
| public class TestActivity extends Activity
{
private static final String SOAP_ACTION = "http://tempuri.org/IService1/Request";
private static final String METHOD_NAME = "Request";
//private static final String METHOD_NAME = "IService1_Request_InputMessage";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://******/webservicema/Service1.svc?wsdl";
//private static final String URL = "http://****/webservicema/WebService1.asmx";
private EditText editText;
private Button button;
private String research="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
//récupération de l'EditText grâce à son ID
editText = (EditText) findViewById(R.id.EditTextPrenom);
//récupération du bouton grâce à son ID
button = (Button) findViewById(R.id.ButtonEnvoyer);
//On applique un écouteur d'événement au clique sur le bouton
button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//on récupère le texte écrit dans l'EditText
research = editText.getText().toString();
//Appel de la fonction SOAP (Webservice)
String soapResult = soap("research", research);
//Affichage du résultat
soapResult = "Résultat du mot clé " + research + " : " + soapResult;
//Toast.makeText(this, "Bonjour " + research + " !", Toast.LENGTH_LONG).show();
((TextView)findViewById(R.id.TextViewHello)).setText(soapResult);
}
}
);
}
//public static SoapObject soap (String property, String value) throws IOException, XmlPullParserException
public static String soap (String property, String value)
{
// Création de la requête SOAP
SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
//Ajout de propriété: addProperty(nom de variable, valeur) -> Le nom de la variable vient du fichier WSDL
if (property != null)
request.addProperty(property, value);
//Toutes les données demandées sont mises dans une enveloppe.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope.VER11);
//Préparation de la requête
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.setOutputSoapObject (request);
HttpTransportSE androidHttpTransport = new HttpTransportSE (URL);
//Ceci est optionnel, on l'utilise pour savoir si nous voulons ou non utiliser
//un paquet "sniffer" pour vérifier le message original (androidHttpTransport.requestDump)
androidHttpTransport.debug = true;
//Envoi de la requête
try {
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call (SOAP_ACTION, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return "erreur IOException";
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return "erreur XmlPullParserException";
}
//Obtention du résultat
try {
String soapResult = envelope.getResponse().toString();
return soapResult;
//return androidHttpTransport.responseDump;
} catch (SoapFault e) {
// TODO Auto-generated catch block
//return e.toString();
return "erreur SoapFault";
}
//return "erreur";
}
} |
Partager