0

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 .

Yanshof
  • 9,659
  • 21
  • 95
  • 195

2 Answers2

2

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:

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • 1
    The version 0.0.4 is out with tutorial: http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/04/16/Version+0.0.4+released http://wiki.javaforum.hu/display/ANDROIDSOAP/Step+by+step+tutorial – Gábor AUTH Apr 27 '12 at 20:05
1
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.

kyogs
  • 6,766
  • 1
  • 34
  • 50