[font=Arial Black]Tutorial Coldfusion 8 Webservice (SOAP) with ANDROID Client (KSOAP) and PHP Client (NUSOAP)[/font]
So I tried the KSOAP with the tutorials and codes from Lex and the ksoap library from tuxpan look at theGoogle Discussion
for a coldfusion webservice with soap (Ksoap for android)
[font=Arial Black]1. Coldfusion Webservice[/font]
The coldfusion webservice is made very easy:
Just download coldfusion for developer it's free and set up the internal webserver
Just administrate it with localhost:8500
After this you made a new folder in the wwwroot folder like mine is DA.
Then into this you make a folder ws_functions. Into this folder you save the test6.cfc componet (this is the webservice) the wsdl file is made automatically for you.
test6.cfc:
- Code: Select all
<cfcomponent >
<!--- Every CFC method published as a web service must have a returntype specified.
If your web service does not return a value, set returntype="void". --->
<cffunction
name="getMessage"
access="remote"
returnType="filename[]"
output="false">
<!--- Optional input parameters are not supported in web services. Therefore,
if you have any cfargument tags with the required attribute set to no,
the setting is ignored and the argument treated as required. --->
<cfargument name="vname" type="string" required="yes">
<cfargument name="nname" type="string" required="yes">
<cfset var response = ArrayNew(1)>
<cfset var temp = "">
<cfobject component="filename" name="tempname">
<cfset tempname.vname = arguments.vname>
<cfset tempname.nname = arguments.nname>
<cfset temp = ArrayAppend(response, tempname)>
<!--- Hier loop um mehrere Angaben zu übertragen --->
<cfloop index = "LoopCount" from = 1 to = 5 >
<cfobject component="filename" name="tempname2">
<cfset tempname2.vname = #LoopCount#>
<cfset tempname2.nname = #LoopCount# >
<cfset temp = ArrayAppend(response, tempname2)>
</cfloop>
<cfreturn response>
</cffunction>
</cfcomponent>
This webservice needs 2 strings vname and nname which means first and lastname for the definition of the complex type which is the returntype you must make another component which is placed in the ws_functions folder called filename.cfc:
- Code: Select all
<cfcomponent>
<cfproperty name="vname" type="string">
<cfproperty name="nname" type="string">
</cfcomponent>
2. PHP NUSOAP CLIENT
After this your webservice can be connected through a new coldfusion site or with php and nusoap - i tried the second and the first one.
The second one you need an xampp or lampp server. (I loaded it from http://www.apachefriends.org)
NuSoap you can load it from Sourceforge or other sites But becareful with php5! Because it implements a native soap lib and the client is called like the same from nusoap so you must download this for php5 and intall it into webfolder Modified Version Download for php5
Then just enter the following code which is a ltitle bit changed from this one Scott Nichol version little bit updated
PLEASE LOOK IN THE scripts for more COMMENTS ...
- Code: Select all
<?php
// Important to set the nusoap lib into the webfolder
require_once "nusoap/lib/nusoap.php";
$params = array(
'vname' => 'chris',
'nname' => 'braun'
);
//Version zur Abfrage
// ohne literal style = cfcomponent ohne document
// IMPORTANT IN VISTA DON'T TRY LOCALHOST ONLY IP IS GOOD! BECAUSE VISTA DON'T let you use webservice code at a local server don't ask me why
$client = new soapclientnusoap("http://192.168.1.7:8500/DA/ws_functions/test6.cfc?wsdl", true);
// ! IMPORTANT i used the changed php 5 nusoap client so it's ... new saopclientnusoap !!! initialization
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('getMessage' ,$params );
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
3. SOAP ENVELOPE RESPONSE
If you call this then you can see what the client sends to the server what he gets back from the server (SOAP ENvelope) This is what it looks like here: The Full Resonse of the SOAP coldfusion web service:
Using xml Syntax Highlighting
- HTTP/1.0 200 OK
- Date: Tue, 27 May 2008 14:17:38 GMT
- Content-Type: text/xml; charset=utf-8
- Connection: close
- Server: JRun Web Server
- <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <ns1:getMessageResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://ws_functions.da">
- <getMessageReturn soapenc:arrayType="ns1:Filename[6]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">braun</nname>
- <vname xsi:type="xsd:string">chris</vname>
- </getMessageReturn>
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">1</nname>
- <vname xsi:type="xsd:string">1</vname>
- </getMessageReturn>
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">2</nname>
- <vname xsi:type="xsd:string">2</vname>
- </getMessageReturn>
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">3</nname>
- <vname xsi:type="xsd:string">3</vname>
- </getMessageReturn>
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">4</nname>
- <vname xsi:type="xsd:string">4</vname>
- </getMessageReturn>
- <getMessageReturn xsi:type="ns1:Filename">
- <nname xsi:type="xsd:string">5</nname>
- <vname xsi:type="xsd:string">5</vname>
- </getMessageReturn>
- </getMessageReturn>
- </ns1:getMessageResponse>
- </soapenv:Body>
- </soapenv:Envelope>
Parsed in 0.008 seconds, using GeSHi 1.0.8.4
So you can see the complex type of filenmae has two strings in it vname and nname.
4. ANDROID CLIENT (KSOAP)
To get a client for android you must import first the ksoap library of tuxpan Download the full android project from Full Project and the modify the Main class like this
Using java Syntax Highlighting
- package com.google.android.test_ws_array;
- import java.io.IOException;
- import java.util.Vector;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.AndroidHttpTransport;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.TextView;
- public class test_ws_array extends Activity {
- private static final String SOAP_ACTION = "getMessage";
- private static final String METHOD_NAME = "getMessage";
- private static final String NAMESPACE = "http://rpc.xml.coldfusion";
- // !!!!! IMPORTANT!!!!! THE URL OF THE CoLDFUSION WEBSERVER NOT LOCALHOST BECAUSE LOCALHOST IS THE ANDROID EMULATOR !!!!!
- private static final String URL = "http://192.168.1.7:8500/DA/ws_functions/test6.cfc?wsdl";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- //CALL the web service method with the two parameters vname and nname
- SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
- request.addProperty("vname", "Christian");
- request.addProperty("nname", "Braun");
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
- envelope.setOutputSoapObject(request);
- AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
- try {
- androidHttpTransport.call(SOAP_ACTION, envelope);
- // Get the SAOP Envelope back and the extract the body
- SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
- Vector XXXX = (Vector) resultsRequestSOAP.getProperty("getMessageReturn");
- //Count of the arrays beneath starting from 0
- //You can see the buildup with the php site with nusoap http://localhost/DA/nusoapclient_test2.php
- int testat = XXXX.size();
- // It depends on how many arrays we have we can get to the attributs of one of them with get(0), get(1) ....
- SoapObject test = (SoapObject) XXXX.get(0);
- //Get the attributes in the array
- String tem = (String) test.getProperty("vname");
- tem = tem + " " + (String) test.getProperty("nname");
- //Just show it in a text area field called lblStatus
- ((TextView)findViewById(R.id.lblStatus)).setText(tem.toString());
- // with androidhttptransport you need a catch block
- } catch(Exception E) {
- ((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
- }
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
The layout.xml looks like this ...
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="STATUS:"
- />
- <TextView android:id="@+id/lblStatus"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="... editing ..."
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
So maybe someone can see if there is a better way to get the things out of the soap envelope? Then PLEASE tell me! Thanxx






