I'm a 27 year old software developer that mainly focuses on software development. As everyone i have taken a side in the Apple vs Android game. My choice is Android because of oh so many reasons :-) By contributing to the Android developers content online ( in the form of writing Android tutorials ), I hope to convince others to make the same decision and help them make nice content for the Android platform. Mark is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Android development tutorial: reading SOAP services!

07.16.2011
| 16644 views |
  • submit to reddit

I have found that in a lot of cases, applications need data ! What a discovery :-) . The next question of course is: Where do i get my data from. Most of the time you can gather your data from a existing source and in a lot of cases you can manipulate the data because you also control that side of the application. More often than you think you can get useful (free) data from existing sources that you don't own. Thats where webservices come in. Webservices can give you usefull data in a lot of formats.

In previous posts is explained how to make you own webservice in PHP and how to read and parse JSON or XML, but another (pretty big) format is SOAP. In this post we will see how you make a application that reads and parses SOAP data into a Android application!

So to sum up what we will do:

  1. Make a call to a SOAP webservice ( this one , yes hello world!)
  2. Read and parse the output
  3. Display the webserver message in the Activity

Well that's more then enough for 1 tutorial i guess :-) Lets get started.

Preparation

First we will have to download the android SOAP client. ( called ksoap )

Note: At the time of writing this the most recent version is 2.5.4 .

  • Just click the most recent version
  • search for the jar file with dependencies.
  • download it by right clicking the link "Raw file"
  • then clicking "Save as ...".
  • Save it inside your project folder so you can link it easily.

Next: add the jar file to the project. ( Project properties -> Java build path -> Add JAR's )

Note: if you dont see the JAR file, click on your project in the Package explorer and press F5 to refresh your workspace.

Modifications in AndroidManifest.xml
In order to make soap calls on a web service exposed on the internet, additional permissions need to be added.

<uses-permission android:name="android.permission.INTERNET" />

 So now lets dive into the code! I commented the code to make sure you understand it.

private static String SOAP_ACTION = "http://tempuri.org/HelloWorld";

private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "HelloWorld";

private static String URL = "http://bimbim.in/Sample/TestService.asmx?WSDL";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//Use this to add parameters
//request.addProperty("Parameter","Value");

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

//Needed to make the internet call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}

// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;

if(result != null){
TextView t = (TextView)this.findViewById(R.id.resultbox);

//Get the first property and change the label text
t.setText("SOAP response:\n\n" + result.getProperty(0).toString());
}

}

Source code

Other info and examples

Here are some more examples that show the implementation of the ksoap library in combination with Android.

 

 

 

References
Published at DZone with permission of Mark Mooibroek, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Gagan Peter replied on Sat, 2012/04/14 - 5:19am

That’s a good tutorial, but I have a problem and didn’t get a solution yet.
I’m calling a web service that returns me a large soap and I’m getting out of memory error in this line “androidHttpTransport.call(SOAP_ACTION, envelope); ”

How can I handle this?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.