Yanshof
Yanshof

Reputation: 9926

How can i make simple call to network web service from android ?

I need to make some simple web service call from android. I know the server ip address - and i know what it the method that i need to call. The server is base on .net platform - and the method that i need to call will return to me simple string that will tell me what is the server web service version.

I don't know how to make this call.

Thanks for any help .

Upvotes: 0

Views: 2913

Answers (2)

kyogs
kyogs

Reputation: 6836

public class SOAPActivity extends Activity {
     private final String NAMESPACE = "http://www.webserviceX.NET/";
        private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
        private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
        private final String METHOD_NAME = "ConvertWeight";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SoapObject soapObject=new SoapObject(NAMESPACE, METHOD_NAME);

        String weight = "700";
        String fromUnit = "Kilograms";
        String toUnit = "Grams";

        PropertyInfo weightProp =new PropertyInfo();
        weightProp.setName("Weight");
        weightProp.setValue(weight);
        weightProp.setType(double.class);
        soapObject.addProperty(weightProp);

        PropertyInfo fromProp =new PropertyInfo();
        fromProp.setName("FromUnit");
        fromProp.setValue(fromUnit);
        fromProp.setType(String.class);
        soapObject.addProperty(fromProp);

        PropertyInfo toProp =new PropertyInfo();
        toProp.setName("ToUnit");
        toProp.setValue(toUnit);
        toProp.setType(String.class);
        soapObject.addProperty(toProp);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());

            TextView tv = new TextView(this);
            tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
            setContentView(tv);

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

    }

This is code for SOAP web service.

Upvotes: 1

Yahia
Yahia

Reputation: 70369

The answer depends on what sort of webservice this is... a SOAP (XML) webservice would need some XML capabilities, easiest option is to use a library (see below for KSOAP2)... a REST webservice might work with pure HTTP (perhaps plus JSON)...

For some sample source code/walkthrough/library/doc regarding all mentioned options on how to call a webservice from Android see:

Upvotes: 2

Related Questions