SOLUTION:
The below method will call the SOAP server with soap message and returns the ByteArray of response.
private byte[] callSOAPServer() {
byte[] result = null;
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 15000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 35000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
/*
* httpclient.getCredentialsProvider().setCredentials( new
* AuthScope("os.icloud.com", 80, null, "Digest"), new
* UsernamePasswordCredentials(username, password));
*/
HttpPost httppost = new HttpPost(SERVER_URL );
httppost.setHeader("soapaction", SOAP_ACTION);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
System.out.println("executing request" + httppost.getRequestLine());
//now create a soap request message as follows:
final StringBuffer soap = new StringBuffer();
soap.append("\n");
soap.append("
// this is a sample data..you have create your own required data BEGIN
soap.append("
soap.append(" \n");
soap.append("" + body);
soap.append(" \n");
soap.append("
/* soap.append(body); */
// END of MEssage Body
soap.append("");
Log.i("SOAP Request", ""+soap.toString());
// END of full SOAP request message
try {
HttpEntity entity = new StringEntity(soap.toString(),HTTP.UTF_8);
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);// calling server
HttpEntity r_entity = response.getEntity(); //get response
Log.i("Reponse Header", "Begin..."); // response headers
Log.i("Reponse Header", "StatusLine:"+response.getStatusLine());
Header[] headers = response.getAllHeaders();
for(Header h:headers){
Log.i("Reponse Header",h.getName() + ": " + h.getValue());
}
Log.i("Reponse Header", "END...");
if (r_entity != null) {
result = new byte[(int) r_entity.getContentLength()]; // read the output message
if (r_entity.isStreaming()) {
DataInputStream is = new DataInputStream(
r_entity.getContent());
is.readFully(result);
}
}
} catch (Exception E) {
Log.i("Exception While Connecting", ""+E.getMessage());
E.printStackTrace();
}
httpclient.getConnectionManager().shutdown(); //shut down the connection
return result;
}
2)You have to parse the output of above function returned byteArray.
for example .,
byte[] initReqrepsonse = callSOAPServer(soapBodymessage );
ByteArrayInputStream bais=new ByteArrayInputStream(initReqrepsonse);
// now parse the xml
as
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
// ResponseParser is XML parser class which will parse the XML output.
ResponseParser myXMLHandler = new ResponseParser();
xr.setContentHandler(myXMLHandler);
Log.i("XML data", bais.toString());
xr.parse(new InputSource(bais));
By this way,you can access Any SOAP webservice methods without using any libraries.
please let me know if any corrections required.
Hi Rama,
ReplyDeleteDo you think this is the best approach for Web Services access?
Hello,
ReplyDeleteI've copied your code in a new Android project but I get some errors on ResponseParser. Eclipse don't recognize it.
And what kind of message is the soapBodyMessage?
Thanks in advance
Thanks a lot, you save my day :)
ReplyDeleteIt can either be done by an organization for its student loan, which she sends out twice a month.
ReplyDeleteEstimates vary from 7 touches to as many as 90 per cent
of its revenue from licensing.
My blog post: Student Loans for People with Bad Credit