How to call a SOAP web service using a simple string (xml in string format)… here is a solution to the problem.
How to call a SOAP web service using a simple string (xml in string format)
I have this string representing XML:
String soapCall="<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> < soap:Body xmlns:m="http://www.example.org/stock"> <m:addDownloadParams> <m:idApp>";
soapCall+=idApp;
soapCall+="<m:versionApp>";
soapCall+=versonApp;
soapCall+="</m:versionApp> <m:os>Android</m:os> </m:addDownloadParams> </soap:Body> </soap:Envelope>";
I have this SOAP web service:
http://stats.mywebsite.com/ws/adddownload
Now, I need to
pass that string to that SOAP web service on Android, but I don’t know the method, I know I need to use httpcliente:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
But I don’t know how to call soapwebservice using that string.
Who has code samples? I can’t find it on google. I don’t want to use the library, I need to do it myself
Thanks
EDIT: This is the code now, but it doesn’t work, I’m getting error 500 Internal server error:
public static byte[] addDownloadIntoServer(){
byte[] result = null;
String SERVER_URL="http://stats.mywebsite.com/ws/server.php";
String SOAP_ACTION="addDownload";
String body="<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\" http://stats.mobincube.com/\"><SOAP-ENV:Body><ns1:addDownloadParams>";
body+="<idApp>" +SectionManager.instance.app_id+"</idApp>";
body+="<versionApp>"+SectionManager.instance.app_version+"</versionApp>";
body+="<source>android</source> <os>Android</os> </ns1:addDownloadParams></SOAP-ENV:Body></SOAP-ENV:Envelope>";
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(" \n");
soap.append(" \n");
soap.append("" + body);
soap.append(" \n");
soap.append(" \n");
/* 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()];
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;
}
Solution
Solution:
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(" \n");
soap.append(" \n");
soap.append("" + body);
soap.append(" \n");
soap.append(" \n");
/* 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()];
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 must parse the output of the byteArray returned by the above function. 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));
This allows you to access any SOAP web service method without a third-party library.
Please let me know if any corrections are needed.