Thursday, October 21, 2010

Android Send Request

Android Send Request

Aa promised this time im writing about android, as I'm officially in love with that platform and being not a big fan of java (J2EE and EJB2.0 drew me nuts many many times), I have to say that the APIs, the Docs and the IDE integration is just fantastic.

are want to deploy serious business apps to android, almost every serious enterprise business app exposes web services and has multiple client interfaces that consume them and display data and results, in the yearly days of android there was no possibility to just send a request to a .NET web service, the only way around that was to write your own implementation of the request and then process and send the data to the remote resource and get an answer and also process it.

Now there is a ksoap2 library for android so things are much much more simple.

This is nothing really to write to so I'l just post the entire code example that's using the w3schools test web service written in NET.

Simple Example

final String SOAP_ACTION = "";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "";
    private static final String URL = 
    TextView textView;
    private Button submitButton;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        textView = new TextView(this);
        textView.setText("Hello, Android");
        this.submitButton = (Button)this.findViewById(; 
        setOnClickListener(new OnClickListener() 
          public void onClick(View view) 
    public void callService()
            try {

            SoapObject request = 
                new SoapObject(NAMESPACE, METHOD_NAME);
            EditText entryEdit = 
            PropertyInfo info = new PropertyInfo();          
            Editable entryText = entryEdit.getText();
            String value = entryText.toString();
            SoapSerializationEnvelope envelope = 
                new SoapSerializationEnvelope(SoapEnvelope.VER11);
            HttpTransportSE  androidHttpTransport = 
                new HttpTransportSE(URL);
  , envelope);

            SoapPrimitive  result = (
                    SoapPrimitive )envelope.getResponse();
            value = result.toString();
            TextView textResult = 
        catch (Exception e)

Now this is just a sample, so I didn't build nice architecture around it, this was kinda not the point (but I was tempted to do so :-) ).

Notice: If you would like to test this code for your self, and use virtual development server and your emulator you must be aware that this will not work as the web service will be deployed on localhost and localhost is your device loopback interface and not the one that's on you PC so this will fail with "connection refused" exception, the best thing to do would be set up some virtual machine and then perform the tests before deploying on a production server.


If you want to write some serious app on android this should help you to get going.

No comments: