Direct Mobile Web Service Access


Mobile applications can directly access Web services without an intermediary proxy server. MobileDirectTestServiceInvoke.java is a simple Java program that uses the InvokeService class we discussed earlier in the chapter and directly calls the ChessMaster Web service without using a proxy server for the invocation.

The Java source code in its entirety for the MobileDirectTestServiceInvoke.java class follows:

 /*  * MobileDirectTestServiceInvoke.java  */ import java.util.StringTokenizer; public class MobileDirectTestServiceInvoke {    public static void main ( String args[] )    { 

Each of the hard-coded values for the Web service endpoint, method name, and method arguments can be easily changed to build a dynamic mobile application capable of invoking any Web service with arbitrary input parameters.

 String servicename = "http://192.168.1.102:9090/hpws/       soap/ChessMaster"; String serviceclass = "ChessMaster"; String method = "makeNextMove"; String rettype = "String"; String names = "board"; String types = "String"; String values = "1:1:2:1:3:1:4:1:5:1:6:1:7:1:8:1:  1:2:2:3:3:3:4:2:5:6:6:2:7:2:8:2:  1:8:2:8:3:8:4:8:5:8:6:8:8:6:8:8:  1:7:2:7:3:6:4:7:5:7:6:7:7:7:8:7:"; // Prepare to invoke the Web service by setting its //  endpoint location, method name, return type, and //  and arguments. InvokeService callWS = new InvokeService (); callWS.setMethod ( method ); callWS.setReturnType ( rettype ); callWS.setWSURL ( servicename ); 

Go through the name, type, and value tuples for each of the arguments to the Web service method using StringTokenizers. Use callWS.setArgs to update the callWS object with each set of extracted tokens.

   callWS.clearArgs ();   StringTokenizer tokenizerNames = new StringTokenizer ( names , "," , false );   StringTokenizer tokenizerTypes = new StringTokenizer ( types , "," , false );   StringTokenizer tokenizerValues = new StringTokenizer ( values , "," , false );   int k = 0;   while ( tokenizerNames.hasMoreTokens () )   {      String tokType =   tokenizerTypes.nextToken ().trim ();      String tokName =   tokenizerNames.nextToken ().trim ();      String tokVal =   tokenizerValues.nextToken ().trim ();      callWS.setArgs ( k , tokType , tokName , tokVal );      k = k + 1;   } 

After all of the arguments have been properly configured, we invoke the Web service by calling the invokeWSMethod method on the callWS object.

       Object callRetObj = callWS.invokeWSMethod ();       String callRetObjString =       ( String ) callRetObj.toString ();       System.out.println ( "Result is: "           + callRetObjString );    } } 

Unfortunately, the support infrastructure necessary for this application is not available on many mobile devices. Supporting SOAP parsing requires a rich set of capabilities that is simply not available on many mobile platforms. For instance, the Java 2 Mobile Edition (J2ME) Connected Limited Device Configuration (CLDC) platform provides only limited string manipulation capabilities in an effort to improve runtime performance and reduce the overall footprint size. This lack of full Java string functionalities present significant issues for the underlying XML parsers upon which the SOAP parser is based.

Additionally, most SOAP parsers require that the entire XML document be resident in memory before the SOAP envelope is parsed. Most mobile platforms are based on Simple API for XML (SAX) parsers that never create a complete object model that is resident in memory.

The kSOAP platform addresses the need for a SOAP parser for mobile devices. kSOAP is built on Enhydra's kXML parser, and presents an efficient platform for mobile SOAP-based messaging. More information about kSOAP can be found at http://ksoap.enhydra.org.

The Java source code in its entirety for the MobileDirectKSoapTestServiceInvoke class follows. This class uses kSOAP to directly invoke the ChessMaster Web service on a remote machine.

 /*  * MobileDirectKSoapTestServiceInvoke.java  */ import org.ksoap.ClassMap; import org.ksoap.SoapObject; import org.ksoap.transport.HttpTransportSE; import java.util.StringTokenizer; public class MobileDirectKSoapTestServiceInvoke {    public MobileDirectKSoapTestServiceInvoke ()    {    }    public Object invokeService ( String serviceName ,                                  String method ,                                  String argNames ,                                  String argValues )    {       Object resultObj = null;       HttpTransportSE tse = null;       SoapObject requestSoapObj;       ClassMap cm;       try       {          tse = new HttpTransportSE ( serviceName , method );          tse.debug = true;          cm = new ClassMap ();          tse.setClassMap ( cm );          requestSoapObj =     new SoapObject ( "http:////tempuri.org//" ,           method ); 

Since the Web service argument names and values are managed as comma-delimited tokens within larger strings, we must first extract tokens from each string. StringTokenizer may not be available on some mobile platforms. In this example, for code simplicity only, we use StringTokenizer objects. Depending on how the arguments to the Web service are managed, StringTokenizer may not be needed at all, or an alternate means of extracting string tokens can be implemented.

      StringTokenizer tokenizerNames = new StringTokenizer (  argNames ,       "," ,       false );      StringTokenizer tokenizerValues = new StringTokenizer (  argValues ,       "," ,       false );      while ( tokenizerNames.hasMoreTokens () )      {         String tokName = tokenizerNames.nextToken ().trim ();         String tokVal = tokenizerValues.nextToken ().trim ();   // Add each argument name and value         requestSoapObj.addProperty ( tokName , tokVal );      }      resultObj = tse.call ( requestSoapObj );   }   catch ( Exception e )   {      System.out.println ( "InvokeService: Error" );   } 

We use the HttpTransportSE object's debug capabilities to output the request SOAP message that is sent from the mobile application to the remote Web service, as well as the response SOAP message that is returned by the remote Web service to the mobile application.

       System.out.println ( "SOAP Request is:\n" +            tse.requestDump );       System.out.println ( "SOAP Response is:\n" +             tse.responseDump );       return resultObj;    }    public static void main ( String args[] )    {       String servicename = "http://192.168.1.102:9090/hpws/            soap/ChessMaster";       String serviceclass = "ChessMaster";       String method = "makeNextMove";       String names = "board";       String values = "1:1:2:1:3:1:4:1:5:1:6:1:7:1:8:1:       1:2:2:3:3:3:4:2:5:6:6:2:7:2:8:2:1:       8:2:8:3:8:4:8:5:8:6:8:8:6:8:8:1:7:       2:7:3:6:4:7:5:7:6:7:7:7:8:7:";       MobileDirectKSoapTestServiceInvoke mdks =   new MobileDirectKSoapTestServiceInvoke ();       String resultString =   mdks.invokeService (  servicename ,         method ,         names ,         values ).toString ().trim ();       System.out.println ( "Result from Web service is: " +            resultString );    } } 

Figure 10-10 shows the SOAP envelope that is generated and sent to the ChessMaster Web service by the MobileDirectKSoapTestServiceInvoke class.

Figure 10-10. The request SOAP envelope for invoking the ChessMaster Web service.
 <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/     2001/XMLSchema-instance"     xmlns:xsd=http://www.w3.org/2001/XMLSchema     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/         soap/encoding/"     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/         soap/envelope/">   <SOAP-ENV:Body     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/         soap/encoding/">     <makeNextMove xmlns="http:////tempuri.org//"          SOAP-ENC:root="1">       <board xmlns="" xsi:type="xsd:string">           1:1:2:1:3:1:4:1:5:1:6:1:7:1:8:1:1:           2:2:3:3:3:4:2:5:6:6:2:7:2:8:2:1:8:           2:8:3:8:4:8:5:8:6:8:8:6:8:8:1:7:2:           7:3:6:4:7:5:7:6:7:7:7:8:7:       </board>     </makeNextMove>   </SOAP-ENV:Body> </SOAP-ENV:Envelope> 

Figure 10-11 shows the SOAP envelope that is returned by the ChessMaster Web service to the MobileDirectKSoapTestServiceInvoke class. On receiving the SOAP envelope, the MobileDirectKSoapTestServiceInvoke class parses the envelope, and sets the value of resultString to "3,6,3,5,".

Figure 10-11. The response SOAP envelope sent by the ChessMaster Web service.
 <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.     xmlsoap.org/soap/envelope/">   <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.       xmlsoap.org/soap/encoding/">     <makeNextMoveResponse xmlns:SOAP-ENC=         "http://schemas.xmlsoap.org/soap/encoding/"         xmlns:xsd="http://www.w3.org/2001/XMLSchema"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-                    instance">        <return xsi:type="xsd:string">3,6,3,5,</return>     </makeNextMoveResponse>   </SOAP-ENV:Body> </SOAP-ENV:Envelope> 

In this section, we saw how to directly invoke Web services from mobile devices. Mobile platforms that have sufficient resources can use more traditional XML and SOAP parsers, while more limited devices can use platforms such as kSOAP and kXML.



Developing Enterprise Web Services. An Architect's Guide
Developing Enterprise Web Services: An Architects Guide: An Architects Guide
ISBN: 0131401602
EAN: 2147483647
Year: 2003
Pages: 141

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net