Wednesday, April 28, 2010

Android kSOAP2 framework

The following code provides a simple kSOAP2 framework for Android bases handsets.  The framework is in the module AndroidkSoap2.  I provies a series of call that allows users to access various web services on the internet.  The code consists of 2 modules,

GetWeather.java -- Andoroid Web Services program that gets current reported weather from a zip code.
AndroidkSoap2.java -- Android Web Services Framework.

There are several other files that include the views, manifest , colors and strings.  You will need to link the kSOAP2 jar file to your program.  The ksoap2 library is called ksoap2-android-assembley-2.4-jar-with-dependencies.jar.  You can find this library in the Android development website.  This should  make your web services life easier with android.


Introduction
kSOAP2 is a stripped down version of SOAP. It contains the necessary methods to build a SOAP object, insert properties, create the SOAP envelope and issue the RPC to the desired website. It also contains methods to recover the returned SOAP object. kSOAP2 allows developers of Android handsets the ability to create Web Services or SOA clients and extract enterprise data from any WS websites.

Group 5.2 has developed a simple kSOAP2 framework that will allow Android developers to easily SOA Web Services client applications for the Android platform. It utilizes the kSOAP2 libraries but allows developer to create a complete web service session with just a single class and 4 methods. It is intuitive utilizing input parameters from the WSDL.


How to Use the kSOAP2 Framework

The framework is easy to use. Simply take the AndroidkSOAP2.java class and include it in your Android package. You will need to link the kSOAP2 library to your project. The library that we used is ksoap2-android-assembly-2.4-jar-withdependencies.jar. This was the latest release as of April 2010. You can get this library from the Android developer’s site and is highlighted in the referenced section in this paper. You will also need to update your manifest with user permissions to enable internet access,

A snippet of code that calls AndroidkSoap2.java follows. It takes you through what is needed to put in your own android code. This snippet of code gets the current weather conditions by zip code.

// Initialize some SOAP Strings

// All of these parameters are gotten from the WSDL

String RequestMethodName = "GetCityWeatherByZIP";

String RequestNameSpace = "http://ws.cdyne.com/WeatherWS/";

String URL = "http://ws.cdyne.com/WeatherWS/Weather.asmx";

String wsVersion = "Ver11";



// Create kSOAP2 Framework Object

AndroidkSoap2 wx = new AndroidkSoap2();



// Initialize SOAP RequestObject

wx.wsSetup(RequestNameSpace, RequestMethodName, wsVersion);



// Stuff SOAP Object with Parameters

wx.wsSetParameters("ZIP", ZipCode)



// Go Get Web Service Return Object and return as a string

String wxString = wx.wsExecuteString(URL);



// Done with framework, now parse the SOAP string





The calls are pretty straight forward. The first is the creation of the AndroidkSoap2 object. This object contains all the necessary soap objects and methods for setup and SOAP internet RPC’s.



AndroidkSoap2 wx = new AndroidkSoap2();



The next method, wsSetup(), sets up the soap object. Here we pass the Namespace, Web Services Method name, and the Web Service Version. These three values are obtained from the WSDL.



wx.wsSetup(RequestNameSpace, RequestMethodName, wsVersion);



Next we stuff the soap object with the necessary parameters. We use the framework method wsSetParameters(). There are two parameters passed, the parameter type and parameter string. These are the parameters required by the SOAP Object. ZipCode is a variable set by user input from the main program GetWeather.java. The parameter type is defined in the WSDL. If you have more than one parameter to stuff into the kSOAP2 object you will have to execute this command several times until you are finished placing all the properties into the SOAP object.



wx.wsSetParameters("ZIP", ZipCode)



Finally we initiate a Web Services request. We use the wsExecuteString() method. Only one parameter is provided, the URL of the Web Service. The returned value for this method is a string version of the SOAP returned object. There is a second wsExecute method called wsExecuteObject() which passes the returned SOAP object. Both are the same object. wsExecuteString() is merely a convenience for converting the WS SOAP object to a string. Most programs will use the string method.



AndroidkSoap2 Framework Definitions

public class AndroidkSoap2() -- Android class that contains all kSOAP2 framework methods. No parameters are passed.



public void wsSetup(String wsNameSpace, String wsMethodName, String wsVer) – kSOAP2 framework method that initializes the kSOAP object.



wsNameSpace Name of Web Service NameSpace. This is provided in the WSDL.

wsMethodName Name of desired Web Service method. This is provided in the WSDL.

wsVer Version of Web Services used at website. This is provided in the WSDL.



public void wsSetParameters(String wsParm1, String wsParm2)— kSOAP2 framework method that places all the necessary parameters in the kSOAP2 object. You may need to execute this multiple times to place all the required parameters in the object


wsParm1 Parameter Type. Provided by the WSDL.

wsParm2 Parameter String. Provided by the WSDL.



public String wsExecuteString(String wsURL) – kSOAP2 framework method that initiates the kSOAP2 RPC command. Returns a string that represents contents of the SOAP object returned from the web service.



public Object wsExecuteObject(String wsURL) – KSOAP2 framework method that initiate the kSOAP2 RPC command. Returns the SOAP object returned from the web service.



wsURL URL of the Web Service.













Android kSOAP2 Web Services Framework Example

Included next is an Android web services example. It is a simple program that takes your zip code and provides the current weather conditions for that area. The code includes the main application along with the resource files. Don’t forget to set internet permissions in the manifest. You will also need to link the kSOAP2 library to your project. Copy this source into the appropriate locations in your Android project and you should be good to go!


GetWeather.java



package fau.edu.getweather;



import fau.edu.getweather.R;

import android.view.View.OnClickListener;

import android.view.View;

import android.widget.TextView;

import android.app.Activity;

import android.os.Bundle;

import android.widget.EditText;

import java.util.regex.Pattern;

import java.util.regex.Matcher;



public class GetWeather extends Activity implements OnClickListener {



// Set up display strings

TextView tv;

EditText zipEditText;

static String ZipCode;

String wxString;



@Override

public void onCreate(Bundle savedInstanceState) {



// Inflate display

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



// Set view id's

tv = (TextView)findViewById(R.id.WXoutput);

zipEditText = (EditText)findViewById(R.id.zipinput);





// Set up click listeners for WX and Exit Buttons

View getWx = findViewById(R.id.go);

getWx.setOnClickListener(this);

View allDone = findViewById(R.id.wxexit);

allDone.setOnClickListener(this);

View allClear = findViewById(R.id.wxclear);

allClear.setOnClickListener(this);



}



// Key Click Handler

public void onClick(View v) {

switch (v.getId()) {



// Exit button pressed, terminate program

case R.id.wxexit:

finish();

break;



// Go button pressed, go get web service weather

case R.id.go:

goGetWX();

break;



// Clear button pressed, clear EditText input fields

case R.id.wxclear:

zipEditText.setText("");

break;

}

}



public void goGetWX() {



// Go get zip code input string

ZipCode = zipEditText.getText().toString();



// Initialize some SOAP Strings

// All of these parameters are gotten from the WSDL

String RequestMethodName = "GetCityWeatherByZIP";

String RequestNameSpace = "http://ws.cdyne.com/WeatherWS/";

String URL = "http://ws.cdyne.com/WeatherWS/Weather.asmx";

String wsVersion = "Ver11";



// Create kSOAP2 Framework Object

AndroidkSoap2 wx = new AndroidkSoap2();



//

//This is where the kSOAP2 Framework is used

//



// Initialize SOAP RequestObject

wx.wsSetup(RequestNameSpace, RequestMethodName, wsVersion);



// Stuff SOAP Object with Parameters

wx.wsSetParameters("ZIP", ZipCode);





// Go Get Web Service Return Object and return as a string

String wxString = wx.wsExecuteString(URL);



//

// Done with framework, now parse the SOAP string

//



// Ok, now let's build the output strings

String screenString = "\n\nCurrent Weather\n" + "\n";

String[] tokens = null;

String splitPattern = ";";

String tempString;



// Get Weather City

Pattern pattern = Pattern.compile("City=.*;");

Matcher matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Get Current Conditions

pattern = Pattern.compile("Description=.*;");

matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Get Temperature

pattern = Pattern.compile("Temperature=.*;");

matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Get Humidity

pattern = Pattern.compile("Humidity=.*; ");

matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Get Wind

pattern = Pattern.compile("Wind=.*; ");

matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Get Atmospheric Pressure

pattern = Pattern.compile("Pressure=.*; ");

matcher = pattern.matcher(wxString);

matcher.find();

tempString = matcher.group();

tokens = tempString.split(splitPattern);

screenString = screenString + tokens[0] + "\n";



// Print out response

tv.setText(screenString);



}





AndroidkSoap2.java



package fau.edu.getweather;



import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.AndroidHttpTransport;





// kSOAP2 Framework....

// Allows easy integration of web services into any Android application.

// Simply place this class into the same or another package

// You will have to link the kSOAP2 Library to the project

// Use library ksoap2-android-assembly-2.4-jar-with-dependancies.jar

///

public class AndroidkSoap2 {



// Initialize some strings -- Nice and private

private String wsVersion;

private String msDotNet = "asmx";

private String wsString;

private String wsAction;

private Object wsResult;

private SoapObject wsObject;



// Setup SOAP object -- This is the first method you execute

// wsNameSpace -- Web Service Name Space: You get this from the WSDL

// wsMethodName -- Web Service request: You get this from the WSDL

public void wsSetup(String wsNameSpace, String wsMethodName, String wsVer) {



// Create SOAP Object

wsObject = new SoapObject(wsNameSpace, wsMethodName);

// Stuff object with the namespace and the WS request

wsAction = wsNameSpace + wsMethodName;

wsVersion = wsVer;

}





// Stuff the SOAP Object with needed parameters required by the MethodName

// wsParm1 -- Parameter type: You get this from the WSDL

// wsParm2 -- Parameter Value: You get this from the WSDL

public void wsSetParameters(String wsParm1, String wsParm2) {



wsObject.addProperty(wsParm1, wsParm2);

}



// Issue WS request -- This method returns a string of the SOAP object

// wsURL -- URL name of web service site: You get this from the WSDL

public String wsExecuteString(String wsURL) {



// First create the SOAP envelope

SoapSerializationEnvelope soapEnvelope;



// Set which version of SOAP you will be using: Get this from the WSDL

if (wsVersion == "Ver11"){

soapEnvelope = new

SoapSerializationEnvelope(SoapEnvelope.VER11);}

else {

soapEnvelope = new

SoapSerializationEnvelope(SoapEnvelope.VER12);}



// Fix for Microsoft Based WS Sites -- errrrrr!

// If the URL has “.asmx” extension then that’s the bad boy

Integer index = wsURL.lastIndexOf(msDotNet);

if (index >= 0){

soapEnvelope.dotNet = true;}



// Serialized the SOAP Object and stuff it in the envelope

soapEnvelope.setOutputSoapObject(wsObject);



// Create the http RPC Object

AndroidHttpTransport aht = new AndroidHttpTransport(wsURL);

try

{

// Make the SOAP RPC call

aht.call(wsAction, soapEnvelope);

// Get the SOAP Response Object

wsResult = soapEnvelope.getResponse();

// Convert Object to a string

wsString = wsResult.toString();

}

catch(Exception e)

{

e.printStackTrace();

}

// All done, return a string of the response object

return wsString;

}





// Issue WS request -- This method returns a SOAP response Object

// wsURL -- URL name of web service site: You get this from the WSDL

public Object wsExecuteObject(String wsURL) {



// First create the SOAP envelope

SoapSerializationEnvelope soapEnvelope;



// Set which version of SOAP you will be using: Get this from the WSDL

if (wsVersion == "Ver11"){

soapEnvelope = new

SoapSerializationEnvelope(SoapEnvelope.VER11);}

else {

soapEnvelope = new

SoapSerializationEnvelope(SoapEnvelope.VER12);}



// Fix for Microsoft Based WS Sites -- errrrrr!

// If the URL has “.asmx” extension then that’s the bad boy

Integer index = wsURL.lastIndexOf(msDotNet);

if (index >= 0){

soapEnvelope.dotNet = true;}



// Serialized the SOAP Object and stuff it in the envelope

soapEnvelope.setOutputSoapObject(wsObject);



// Create the http RPC Object

AndroidHttpTransport aht = new AndroidHttpTransport(wsURL);

try

{

// Make the SOAP RPC call

aht.call(wsAction, soapEnvelope);

// Get the SOAP Response Object

wsResult = soapEnvelope.getResponse();

}

catch(Exception e)

{

e.printStackTrace();

}

// All Done, return the response object

return wsObject;

}



}

1 comment: