Results 1 to 3 of 3
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Understanding a program for calling a SOAP service in android

    I am new to learning android development, and I need to call SOAP webservices in a project, I have been looking into tutorials and already written code, I picked up this piece of code and I am trying to understand it. I have searched quite much, but "the more I read to resolve a confusion, the more confusions I get" is my all time problem.

    If somebody can guide me with the following questions, I will be very very very grateful, nothing though it might mean, but I have been trying alot to make my mind clear (while taking care of the time I have) , and in the end I have posted a question here.




    1. Line # 39:- NAMESPACE is required by web services so that they don't mix each other's schemas and all with each other. So they should be globally unique. But does it stay unique when we use tempuri.org? and Can I use tempuri.org as a namespace in all of my projects?
    2. Line # 39:- What is METHOD_NAME1? I don't see any particular method named FahrenheitToCelsius anywhere later in the program? Is it in the web service API to actually do the conversion?
    3. Line # 47:- The ksoap2 API describes SoapSerializationEnvelope as "This class extends the SoapEnvelope with Soap Serialization functionality." Can somebody please tell me what is soap serialization functionality? Although I have just kept in mind that this line needs to be here and we specify the version of ksoap2 with it. Is that right?
    4. SOAP ENVELOPE, SOAP SERIALIZATION ENVELOPE: - What is an envelope basically? ksoap2 describes a SoapEnvelope as "A SOAP envelope, holding head and body objects." What are head and body objects? Should I simply take them as objects, can hold any data and methods..?
    5. Line # 51:- dotnet: " Set this variable to true for compatibility with what seems to be the default encoding for .Net-Services."
    6. Line # 60:- SOAP_ACTION1(tempuri - Bing is the URI of the function which will convert our Fahrenheit value to Celsius value?) is where the Soap web service lies, The result of the function FahrenheitToCelsius is saved in envelope object and returned. Is that right?
    7. Line # 64:-
      Java Code:
      [COLOR=#000000]envelope.bodyIn[/COLOR]
      is the returned value (see previous point),
      Java Code:
      (SoapObject) envelope.bodyIn
      casts the envelope to an instance of SoapObject. Is that right?

    8. Line # 70 to 77:- What don't they start the UI thread by
      Java Code:
      runOnUiThread(new runnable(){...}).start();
    9. Java Code:
      [FONT=verdana][/FONT]
    10. Line # 82:- What does it do?


    Java Code:
    package com.webservice;
    
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class WebServiceDemoActivity extends Activity {
    
    
        /** Called when the activity is first created. */
        private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
        private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
        private static String NAMESPACE = "http://tempuri.org/";
        private static String METHOD_NAME1 = "FahrenheitToCelsius";
        private static String METHOD_NAME2 = "CelsiusToFahrenheit";
        private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
    
    
        private EditText txtFar;
        private EditText txtCel;
    
    
        private Runnable farToCelTask = new Runnable() {
    
    
            @Override
            public void run() {
                // Initialize soap request + add parameters
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
    
    
                // Use this to add parameters
                request.addProperty("Fahrenheit", txtFar.getText().toString());
    
    
                // Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    
    
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;
    
    
                try {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    
    
                    // this is the actual part that will call the
                    // webservice
                    androidHttpTransport.call(SOAP_ACTION1, envelope);
    
    
                    // Get the SoapResult from the envelope body.
                    final SoapObject result = (SoapObject) envelope.bodyIn;
    
    
                    if (result != null) {
                        // Get the first property and change the label
                        // text
                        runOnUiThread(new Runnable() {
    
    
                            @Override
                            public void run() {
                                txtCel.setText(result.getProperty(0).toString());
                            }
                        });
                    } else {
                        showStdError();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    
    
        private Runnable celToFarTask = new Runnable() {
    
    
            @Override
            public void run() {
                // Initialize soap request + add parameters
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
    
    
                // Use this to add parameters
                request.addProperty("Celsius", txtCel.getText().toString());
    
    
                // Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    
    
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;
    
    
                try {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    
    
                    // this is the actual part that will call the
                    // webservice
                    androidHttpTransport.call(SOAP_ACTION2, envelope);
    
    
                    // Get the SoapResult from the envelope body.
                    final SoapObject result = (SoapObject) envelope.bodyIn;
    
    
                    if (result != null) {
                        // Get the first property and change the label
                        // text
                        runOnUiThread(new Runnable() {
    
    
                            @Override
                            public void run() {
                                txtFar.setText(result.getProperty(0).toString());
                            }
                        });
                    } else {
                        showStdError();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web_service_demo);
    
    
            txtFar = (EditText) findViewById(R.id.txtFar);
            txtCel = (EditText) findViewById(R.id.txtCel);
        }
    
    
        public void onFar(View v) {
            executeProgressTask(farToCelTask);
        }
    
    
        public void onCel(View v) {
            executeProgressTask(celToFarTask);
        }
    
    
        private void executeProgressTask(Runnable rtask) {
            ProgressTask task = new ProgressTask(this, "Converting", "wait please...", rtask);
            task.execute();
        }
    
    
        public void onClear(View v) {
            txtCel.setText("");
            txtFar.setText("");
        }
    
    
        private void showStdError() {
    
    
            runOnUiThread(new Runnable() {
    
    
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "No Response", Toast.LENGTH_LONG).show();
                }
            });
        }
    
    
    
    }[FONT=verdana][/FONT]

    Last edited by Nazneen Ali; 11-13-2013 at 08:18 PM.
    Don't forget to smile :-)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,096
    Rep Power
    20

    Default Re: Understanding a program for calling a SOAP service in android

    1. This tells me you really ought to read up on XML and XSDs. That's what underlies this and will show you what things like namespaces represent. You are correct in that they are there to try and prevent naming clashes, but you ought to read up on it in the context in which they originate.

    2. Yes, this is the name used by the web service to determine what, amongst its available methods, you want to actually do.

    3. No idea about the structure of ksoap.

    4. This is pretty basic SOAP stuff. You should be able to read up on this. Go to the horses mouth. Yes, these are rather dry documents (though I have read worse), but they define the technology. Start with the primer. Part 1 has the definition of the message (including the envelope).

    5. I suspect so. Different frameworks can define certain code structures differently in XML. Arrays and collections have caused me issues before now.

    6. It does look like it reuses the envelope object to handle the returned data. Not convinced that's the way other frameworks will function.

    7. See 6.

    8. The UI thread is already running. This is how you tell Android to run code on the UI thread, which is where you are supposed to run anything that affects the UI.

    9. Pardon?

    10. Which line is line 82? The one in the code you posted is a '}'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Understanding a program for calling a SOAP service in android

    @Tolls: Thank you very much.

    I have myself forgotten about 9 and 10.
    Don't forget to smile :-)

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2013, 02:37 PM
  2. Replies: 0
    Last Post: 02-15-2012, 01:28 PM
  3. Transfer a file from Web Service to Android
    By vannam28071988 in forum Android
    Replies: 1
    Last Post: 11-16-2011, 12:00 AM
  4. Replies: 0
    Last Post: 09-27-2011, 03:46 PM
  5. Android read/write to database on a web service.
    By SeanieSortMeOut in forum Android
    Replies: 1
    Last Post: 02-05-2011, 01:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •