Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.os.AsyncTask;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


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 MainActivity extends Activity {

    private final String NAMESPACE = "http://tempuri.org/Webservice";
    private final String URL = "http://localhost:28013/CalculatorWebService.asmx";
    private final String SOAP_ACTION = "http://tempuri.org/Webservice/Add";
    private final String METHOD_NAME = "Add";
    private String TAG = "PGGURU";
    private static String fstNumber;
    private static String secNumber;
    private static String ans;
    Button b;
    EditText fn;
    EditText sn;
    TextView tv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fn = (EditText) findViewById(R.id.et1);
        sn = (EditText) findViewById(R.id.et2);
        tv = (TextView) findViewById(R.id.tv_result);

        b = (Button) findViewById(R.id.btn);

        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Check if FirstNumber text control is not empty
                if (fn.getText().length() != 0 && fn.getText().toString() != "") {
                    //Get the text control value
                    fstNumber = fn.getText().toString();
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute
                    task.execute();
                    //If text control is empty
                } else {
                    tv.setText("Please enter some numbers");
                }
                if (sn.getText().length() != 0 && sn.getText().toString() != "") {
                    //Get the text control value
                    secNumber = sn.getText().toString();
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute
                    task.execute();
                    //If text control is empty
                } else {
                    tv.setText("Please enter some numbers");
                }
            }

        });

    }

    public void getAdd(String firstNumber, String secondNumber) {
        //Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //Property which holds input parameters
        PropertyInfo PI = new PropertyInfo();
        //Set Names
        PI.setName("firstNumber");
        //Set Values
        PI.setValue(firstNumber);
        //Set dataType
        PI.setType(Double.class);
        //Add the property to request object
        request.addProperty(PI);
        // second Property which holds input parameters
        PI = new PropertyInfo();
        //Set Names
        PI.setName("secondNumber");
        //Set Values
        PI.setValue(secondNumber);
        //Set dataType
        PI.setType(Double.class);
        //Add the property to request object
        request.addProperty(PI);
        //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 {
            //Involve web service
            androidHttpTransport.call(SOAP_ACTION, envelope);
            //Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            //Assign it to ans static variable
            ans = response.toString();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private class AsyncCallWS extends AsyncTask<string,> {




        @Override
        protected Void doInBackground(String... params) {
            
            Log.i(TAG, "doInBackground");
            getAdd(fstNumber,secNumber);
            return null;
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
            tv.setText("Calculating...");
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Log.i(TAG, "onPostExecute");
            tv.setText(ans + "is the answer");
        }



        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }
Posted
Updated 4-May-15 9:28am
v2
Comments
Richard MacCutchan 4-May-15 12:48pm    
Please do not just dump a lot of unformatted code and expect someone to make it work. Give proper details of your problem and just show the areas of code that do not work correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900