Ksoap2 Header Example For Essay

iOs Example

When you upload your WSDL file or submit your service URL we use our engine to create the files needed to connect and automatically receive a parsed object. Our goal is to make the integration process easier and faster for you.

Test our service with our example service parsing

AUTO Generated Code Example


1. Add Wsdl2CodeDelegate to Your Class
2. Implement Wsdl2CodeDelegate Methods
3. Add libxml.a to your project
4. Add libxml header files to the search path - $(SDK_DIR)"/usr/include/libxml2
* Download The Example Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29//if service recieve an error this method will be called-(void)proxyRecievedError:(NSException*)exInMethod:(NSString*)method{ NSLog(@"Exeption in service %@",method); } //proxy finished, (id)data is the object of the relevant method service-(void)proxydidFinishLoadingData:(id)dataInMethod:(NSString*)method{ NSLog(@"Service %@ Done!",method); } - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; SampleServiceProxy *proxy = [[SampleServiceProxy alloc]initWithUrl:@"YOUR URL"AndDelegate:self]; [proxy GetDouble]; [proxy GetEnum]; [proxy getEnum:kTestEnumTestEnum2]; [proxy GetInt16]; [proxy GetInt32]; [proxy GetInt64]; [proxy GetString]; [proxy getListStrings]; // the answer will be in the delegate method

Auto Generated Proxy h File

Auto Generated Proxy m File

#import <Foundation/Foundation.h>#import "xpathquery.h"#import "SampleService.h"#import "Response.h"#import "Request.h"#ifndef _Wsdl2CodeProxyDelegate#define _Wsdl2CodeProxyDelegate@protocolWsdl2CodeProxyDelegate//if service recieve an error this method will be called-(void)proxyRecievedError:(NSException*)exInMethod:(NSString*)method; //proxy finished, (id)data is the object of the relevant method service-(void)proxydidFinishLoadingData:(id)dataInMethod:(NSString*)method; @end#endif@interfaceSampleServiceProxy : NSObject@property (nonatomic,assign) id<Wsdl2CodeProxyDelegate> proxyDelegate; @property (nonatomic,copy) NSString* url; @property (nonatomic,retain) SampleService* service; -(id)initWithUrl:(NSString*)urlAndDelegate:(id<Wsdl2CodeProxyDelegate>)delegate; ///Origin Return Type:String-(void)HelloWorld; ///Origin Return Type:Response-(void)ServiceSample:(Request *)req ; ///Origin Return Type:String-(void)getListStrings; ///Origin Return Type:WSPerson-(void)GetListOfCustomObject; ///Origin Return Type:String-(void)GetString; ///Origin Return Type:Int32-(void)GetInt32; ///Origin Return Type:Int16-(void)GetInt16; ///Origin Return Type:Int64-(void)GetInt64; ///Origin Return Type:Double-(void)GetDouble; ///Origin Return Type:Int64-(void)GetLong; ///Origin Return Type:TestEnum-(void)GetEnum; ///Origin Return Type:-(void)GetVoid; ///Origin Return Type:String-(void)sendEnum:(TestEnum)enum1 ; ///Origin Return Type:TestEnum-(void)getEnum:(TestEnum)enum2 ; @end
//------------------------------------------------------------------------------// <wsdl2code-generated>// This code was generated by http://www.wsdl2code.com iOs version Beta 1.0//// Please dont change this code, regeneration will override your changes//</wsdl2code-generated>////------------------------------------------------------------------------------////This source code was auto-generated by Wsdl2Code Beta Version//#import "SampleServiceProxy.h"@implementationSampleServiceProxy-(id)initWithUrl:(NSString*)urlAndDelegate:(id<Wsdl2CodeProxyDelegate>)delegate{ self = [super init]; if (self !=nil){ self.service = [[SampleService alloc] init]; [self.service setUrl:url]; [self setUrl:url]; [self setProxyDelegate:delegate]; } return self; } -(void)dealloc{ [_url release]; [_service release]; [super dealloc]; } ///Origin Return Type:String-(void)HelloWorld{ [self.service addTarget:self AndAction:&HelloWorldTarget]; [self.service HelloWorld]; } void HelloWorldTarget(SampleServiceProxy* target, id sender, NSString* xml){ @try

This is a basic KSOAP Android tutorial - here I will show you how to get started with KSOAP on Android. As you may know, we often want to access Web services via hand-held devices, and most likely you will run into trouble parsing the WSDL and the SOAP messages. Since I come from a .NET background, once I started developing on Android, I realized how much work has been Visual Studio doing for me.

That thought took me to search for a framework or library to help me consume Web Services with Android. I ran into KSOAP2, which seemed like a good library, but unfortunately, very badly documented for most scenarios, like passing or returning complex objects, working with arrays of objects or even working with dates.

All of this I needed to find out by myself and this is why I decided to write this tutorial. So, let's begin.

Getting Started with KSOAP on Android

First things first, so you should now go ahead and download the KSOAP library from Sourceforge Google code (*UPDATE* thanks Freddy):

Then copy and paste the KSOAP library in the folder where your Android project will reside. Open Eclipse, start a new Android Project, right-click on the project's name and choose Properties, like this:

The next thing you need to do is to Add the KSOAP .JAR into the Android Project:

Go ahead an press Add JARs... button. Then navigate to the folder where your KSOAP library is and select it. Once you have this done, you are ready to start working with your Web Service library.

Simple Web Service Calls with KSOAP

First, let's take a look at our web service call in Visual Studio:

[WebService(Namespace = "http://vladozver.org/")] publicclass SimpleWebServices : System.Web.Services.WebService { [WebMethod] publicint GetSumOfTwoInts(int Operand1, int Operand2 ) { return Operand1 + Operand2; } }
The service is deployed on my local machine which is on the address: Pay attention on the ending "/" in the Namespace.

Now, KSOAP finally.

KSOAP relies on a basic object called SoapObject. For this SoapObject, there are 3 variables that are important:
The Web Service Namespace
The Web Service Method Name
The Web Service URL

There is another extra variable which is important and is called SOAP_ACTION, but that is basically a concatenation of the Namespace and Method name:


For now we will create 3 strings for the respective variables:

String NAMESPACE = "http://vladozver.org/"; String METHOD_NAME = "GetSumOfTwoInts"; String SOAP_ACTION = "http://vladozver.org/GetSumOfTwoInts"; String URL = "";
Then we will create the SoapObject:
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Parameters in KSOAP are passed via PropertyInfo class instances, so we will create some of those:

PropertyInfo pi = new PropertyInfo(); pi.setName("Operand1"); pi.setValue(2); pi.setType(int.class); Request.addProperty(pi); PropertyInfo pi2 = new PropertyInfo(); pi2.setName("Operand2"); pi2.setValue(5); pi2.setType(int.class); Request.addProperty(pi2);
Then we will create another important KSOAP object, and that is Soap Envelope:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(Request);
Because our Web Service is .NET based, we need to set the .dotNet property to true.
Next step is to create a Transport object:

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
Lastly, we need to invoke the web service and obtain the result:

try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapObject response = (SoapObject)envelope.getResponse(); int result = Integer.parseInt(response.getProperty(0).toString()); } catch(Exception e) { e.printStackTrace(); }

That is basically it! I hope you will find this tutorial helpful, as I see that many people are looking for a simple Getting started tutorial on KSOAP Android. For more advanced usage of KSOAP, refer to my other posts on this topic.

0 Replies to “Ksoap2 Header Example For Essay”

Lascia un Commento

L'indirizzo email non verrĂ  pubblicato. I campi obbligatori sono contrassegnati *