با سلام
وب سرویس soap چیست ؟
SOAP یک پروتکل ساده بر پایه XML برای نمایش و بسته بندی اطلاعات است که به برنامه های مختلف امکان تعریف و ارسال اطلاعات با استفاده از پروتکل HTTP را می دهد. کاربرد اصلی SOAP بعنوان پرتکل استاندارد برای انتقال پیغام در وب سرویس ها است. به زبان ساده تر SOAP پرتکل دسترسی به وب سرویس ها است.
این وب سرویس تقریبا در حال منسوخ شدن است و اکثرا از وب سرویس Restfull به دلیل سرعت و سهولت پیاده سازی استفاده می کنند.
در ادامه یک نمونه مثال کاربردی از استفاده soap برای اتصال به وب را برای شما عزیزان شرح خواهیم داد.
در این مثال از api آماده سایت W3SCHOOLS استفاده میکنیم.
برای ارتباط با وب سرویس soap در اندروید مراحل زیر را انجام دهید :
کد های زیر را داخل فایل main.xml کپی نمایید :
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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:text="Celsius to Farenheit" android:textSize="30dp" /> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:numeric="integer" android:singleLine="true" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:text="Convert to Farenheit" /> <TextView android:id="@+id/tv_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="" android:textSize="26dp"/> </LinearLayout> |
در ابتدای کلاس webservice.java مقادیر زیر را تعریف کنید
1 2 3 4 5 6 7 8 9 10 |
private final String NAMESPACE = "http://www.w3schools.com/webservices/"; private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; private final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit"; private final String METHOD_NAME = "CelsiusToFahrenheit"; private String TAG = "PGGURU"; private static String celcius; private static String fahren; Button b; TextView tv; EditText et; |
در متد onCreate کلاس فوق کد های زیر را قرار دهید :
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 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Celcius Edit Control et = (EditText) findViewById(R.id.editText1); //Fahrenheit Text control tv = (TextView) findViewById(R.id.tv_result); //Button to trigger web service invocation b = (Button) findViewById(R.id.button1); //Button Click Listener b.setOnClickListener(new OnClickListener() { public void onClick(View v) { //Check if Celcius text control is not empty if (et.getText().length() != 0 && et.getText().toString() != "") { //Get the text control value celcius = et.getText().toString(); //Create instance for AsyncCallWS AsyncCallWS task = new AsyncCallWS(); //Call execute task.execute(); //If text control is empty } else { tv.setText("Please enter Celcius"); } } }); } |
در ادامه کلاس داخلی AsyncCallWS () را در webservice.java تعریف نمایید :
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 |
private class AsyncCallWS extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { Log.i(TAG, "doInBackground"); getFahrenheit(celcius); return null; } @Override protected void onPostExecute(Void result) { Log.i(TAG, "onPostExecute"); tv.setText(fahren + "° F"); } @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute"); tv.setText("Calculating..."); } @Override protected void onProgressUpdate(Void... values) { Log.i(TAG, "onProgressUpdate"); } } |
و در پایان متد getFahrenheit را که وب سرویس ما را call می کند
در کلاس قراردهید :
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 |
public void getFahrenheit(String celsius) { //Create request SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //Property which holds input parameters PropertyInfo celsiusPI = new PropertyInfo(); //Set Name celsiusPI.setName("Celsius"); //Set Value celsiusPI.setValue(celsius); //Set dataType celsiusPI.setType(double.class); //Add the property to request object request.addProperty(celsiusPI); //Create envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; //Set output SOAP object envelope.setOutputSoapObject(request); //Create HTTP call object HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { //Invole web service androidHttpTransport.call(SOAP_ACTION, envelope); //Get the response SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); //Assign it to fahren static variable fahren = response.toString(); } catch (Exception e) { e.printStackTrace(); } } |
در فایل manifest برنامه مجوز اینترنت را بدهید:
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
شما بر چه اساسی میگی روش soap داره منسوخ میشه
بنظر من که از rest قابل اطمینان بره چون یه پروتکله و دارای هزار نوع استاندارد ولی rest یجورایی میشه گفت بیشتر شبیه یه ایدس
فقط به خاطر اسونی تو کار نمیشه به هر چیزی اطمینان کرد
سلام
خب بقیش کو؟