[Solved] How to Use Web Services in Android Application? [closed]


Amit you haven’t searched well on google for this because if you did so you didn’t ask this question here.There are lot of tutorial for this on net if let yourself search on net.Anyways i am posting the code snippet to demonstrate that how you can call the webservice from android…This code is to call only SOAP webservice.To call other webservices like JSON,REST etc search yourself on net.

CODE:-

public class HelloWebService extends Activity{

    String SOAP_ACTION="http://tempuri.org/HelloWorld";
    String METHOD_NAME = "HelloWorld";
    String NAMESPACE = "http://tempuri.org/";
    String URL = "http://192.168.1.15:80/himanshu/helloworldwebservice.asmx";
    String SUM_SOAP_ACTION="http://tempuri.org/AddNumbers";
    String METHOD_NAME1 = "AddNumbers";

    TextView tv1,tv2,tv3,tv4,tv5;
    EditText etA,etB,etName;
    Button bt,dis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello);

        etName = (EditText)findViewById(R.id.et);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
        tv3 = (TextView)findViewById(R.id.tv3);
        tv4 = (TextView)findViewById(R.id.tv4);
        tv5 = (TextView)findViewById(R.id.tv5);
        etA = (EditText)findViewById(R.id.editA);
        etB = (EditText)findViewById(R.id.editB);
        bt =  (Button)findViewById(R.id.add);
        dis = (Button)findViewById(R.id.display);

        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                sum();
            }
        });

        dis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Hello();    
            }
        });

    }

    public void Hello(){

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        Log.d("request", request.toString());

        String str = etName.getText().toString();
        Log.d("str", str);

        request.addProperty("name", str);
        Log.d("request", request.toString());

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Log.d("envelope", envelope.toString());
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        Log.d("envelope", envelope.toString());
        HttpTransportSE aht = new HttpTransportSE(URL);
        aht.debug=true;
        Log.d("aht", aht.toString());

        try
        {
            aht.call(SOAP_ACTION, envelope);
            SoapPrimitive results = (SoapPrimitive)envelope.getResponse();
            Log.d("result", results.toString());
            tv1.setText(""+results.toString());
        }
        catch (Exception e)
        {
            tv2.setText(e.getClass().toString());
            Log.d("Error",e.getClass().toString());
        }

    }

    public void sum(){

            SoapObject sum_request = new SoapObject(NAMESPACE, METHOD_NAME1);
            Log.d("sum_request", sum_request.toString());

            //PropertyInfo pro1 = new PropertyInfo();
            String strA = etA.getText().toString();
            String strB = etB.getText().toString();
            sum_request.addProperty("a", strA);
            sum_request.addProperty("b", strB);

            Log.d("sum_request", sum_request.toString());

            SoapSerializationEnvelope sum_envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            Log.d("sum_envelope", sum_envelope.toString());

            sum_envelope.dotNet = true;
            sum_envelope.setOutputSoapObject(sum_request);
            Log.d("sum_envelope", sum_envelope.toString());

            HttpTransportSE sum_aht = new HttpTransportSE(URL);
            sum_aht.debug=true;
            Log.d("sum_aht", sum_aht.toString());

            try
            {
                sum_aht.call(SUM_SOAP_ACTION, sum_envelope);
                SoapPrimitive sum_results = (SoapPrimitive)sum_envelope.getResponse();
                Log.d("sum_result", sum_results.toString());
              //  int in = Integer.parseInt(sum_results.getProperty(0).toString());
                tv3.setText(""+sum_results.toString());
            }
            catch (Exception e)
            {
                tv3.setText(e.getClass().toString());
                Log.d("sum_error", e.getClass().toString());
            }

        }

    }

7

solved How to Use Web Services in Android Application? [closed]