Monday, January 14, 2013

How to Android Salesforce - Setting up SessionHeader for subsequent calls after login

SessionHeader with sessionId required

Except the initial login call, subsequent Web Service call to Salesforce requires a Soap header (i.e. with name “SessionHeader”) that contains a valid session ID.

Retrieve sessionId from Login result …

With reference to previous blog post, the sessionId and Salesforce API Web Service end point are retrieved from the login call’s response.

SoapObject result=(SoapObject)envelope.getResponse();
String serverUrl=result.getProperty("serverUrl").toString();
String sessionId=getSoapObject().getProperty("sessionId").toString();


Create a SessionHeader and add to Soap Envelope ...

The following snippet shows creation of a Soap Envelope and creation of a Soap Header element named as “SessionHeader” with sessionId. Now, the Soap Envelope is ready to make subsequent calls to Salesforce.

SoapSerializationEnvelope envelope2=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope2.setOutputSoapObject(binding);
Element[] headerOut = new Element[1];
Element sessionHeader = new Element().createElement(NAMESPACE,"SessionHeader");
Element sessionIdNode=sessionHeader.createElement(NAMESPACE, "sessionId");
sessionIdNode.addChild(Node.TEXT, sessionId);
sessionHeader.addChild(Node.ELEMENT,sessionIdNode);
headerOut[0]=sessionHeader;
envelope2.headerOut=headerOut;