Pages

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 = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = 
        "http://www.w3schools.com/webservices/tempconvert.asmx";
        
    
    TextView textView;
    private Button submitButton;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            
        textView = new TextView(this);
        textView.setText("Hello, Android");
        
        this.submitButton = (Button)this.findViewById(R.id.ok); 
        this.submitButton.
        setOnClickListener(new OnClickListener() 
        {
          public void onClick(View view) 
          {
            callService();
          }
          
        });
    }
    
    public void callService()
    {
            try {

            SoapObject request = 
                new SoapObject(NAMESPACE, METHOD_NAME);
           
            EditText entryEdit = 
                (EditText)this.findViewById(R.id.entry);                 
            PropertyInfo info = new PropertyInfo();          
            Editable entryText = entryEdit.getText();
           
            String value = entryText.toString();
                      
            info.setName("Celsius");
            info.setValue(value);
            request.addProperty(info);
    
            SoapSerializationEnvelope envelope = 
                new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
    
            HttpTransportSE  androidHttpTransport = 
                new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive  result = (
                    SoapPrimitive )envelope.getResponse();
            
            value = result.toString();
              
            TextView textResult = 
                (TextView)this.findViewById(R.id.result);
            textResult.setText(value);
            
        }
        catch (Exception e)
        {
            textView.setText(e.getMessage());
        }
    }

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 asp.net 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.

Summary


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

No comments:

 
ranktrackr.net