Sunday 30 October 2011

ksoap.java for accessing ksoap webservice in android


package com.OfficeDepot;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.util.Log;

public class Ksoap {

public static SoapObject CallService(String METHOD_NAME,
int total_parameter, PropertyInfo total_property[]) {

String NAMESPACE = "http://www.officemachinedepot.com/WebService/";

String SOAP_ACTION = NAMESPACE + METHOD_NAME;

String URL = "http://www.officemachinedepot.com/WebService/AceDepotdata.asmx";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

for (int x = 0; x < total_parameter; x++) {
total_property[x].setNamespace(NAMESPACE);
request.addProperty(total_property[x]);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();

envelope.setOutputSoapObject(request);

// Make the soap 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 Soapresponse = (SoapObject) envelope.bodyIn;
return Soapresponse;

}

private static Element buildAuthHeader() {
String NAMESPACE = "http://www.officemachinedepot.com/WebService/";
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "Username");
username.addChild(Node.TEXT, "put your username");
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "Password");
pass.addChild(Node.TEXT, "put your password");
h.addChild(Node.ELEMENT, pass);
return h;
}

}

No comments:

Post a Comment