Asynchronous web service requests

Asynchronous web service requests are useful when the mobile web service application is only occasionally connected to a network. With this method, a web service request is made by calling a method on the service binding class to place the request in an outgoing queue. The method returns a WSResult, which can be used to query the status of the response at a later time, even after the application has been restarted.

The following example makes an asynchronous request to get the USD-to-CAD exchange rate:

// C#
WSResult r = service.AsyncConversionRate( Currency.USD, Currency.CAD );
  
// Get the request ID.  Save it for later use if necessary.
string reqID = r.GetRequestID();

// Later: get the response for the specified request ID
WSResult r = service.GetResult( reqID );
if( r.GetStatus() == WSStatus.STATUS_RESULT_AVAILABLE ) {
  Console.WriteLine( "The conversion rate is " + r.GetDoubleValue( "ConversionRateResult" ) );
} else {
  Console.WriteLine( "Response not available" );
} 
// Java
WSResult r = service.asyncConversionRate( NET.webserviceX.Currency.USD, NET.webserviceX.Currency.CAD );
     
// Get the request ID.  Save it for later use if necessary.
String reqID = r.getRequestID();

// Later: get the response for the specified request ID
WSResult r = service.getResult( reqID );
if( r.getStatus() == WSStatus.STATUS_RESULT_AVAILABLE ) {
    System.out.println( "The conversion rate is " + r.getDoubleValue( "ConversionRateResult" ) );
} else {
    System.out.println( "Response not available" );
}

It is also possible to use a WSListener to get an asynchronous callback when the response to a web service request is available. For example:

// C#
// Make a request to get the USD to CAD exchange rate
WSResult r = service.AsyncConversionRate( Currency.USD, Currency.CAD );
  
// Register a listener for the result
service.SetListener( r.GetRequestID(), new CurrencyConvertorListener() );

// Java
// Make a request to get the USD to CAD exchange rate
WSResult r = service.asyncConversionRate( NET.webserviceX.Currency.USD, NET.webserviceX.Currency.CAD );
     
// Register a listener for the result
service.setListener( r.getRequestID(), new CurrencyConvertorListener() );

The WSListener interface defines two methods for handling asynchronous events:

  • OnResult   An OnResult method is implemented to handle a response to a web service request. It is passed a WSResult object that represents the result of the web service request.

  • OnException   An OnException method is implemented to handle errors that occurred during processing of the response to the web service request. It is passed a WSException object and a WSResult object. The WSException object contains information about the error that occurred, and the WSResult object can be used to obtain the request ID that the response corresponds to.

// C#
class CurrencyConvertorListener : WSListener
{
   public CurrencyConvertorListener() {
   }

   public void OnResult( WSResult r ) {
    try {
         USDToCAD._statusMessage = "USD to CAD currency exchange rate: " + r.GetDoubleValue( "ConversionRateResult" );
    } catch( Exception exc ) {
        USDToCAD._statusMessage = "Request " + r.GetRequestID() + " failed: " + exc.Message;
    }
   }

   public void OnException( WSException exc, WSResult r ) {
    USDToCAD._statusMessage = "Request " + r.GetRequestID() + " failed: " + exc.Message;
   }
} 
// Java
private class CurrencyConvertorListener implements WSListener
{
  public CurrencyConvertorListener() {
  }
 
  public void onResult( WSResult r ) {
   try {
        USDToCAD._statusMessage = "USD to CAD currency exchange rate: " + r.getDoubleValue( "ConversionRateResult" );
   } catch( Exception exc ) {
        USDToCAD._statusMessage = "Request " + r.getRequestID() + " failed: " + exc.getMessage();
   }
   }

   public void onException( WSException exc, WSResult r ) {
        USDToCAD._statusMessage = "Request " + r.getRequestID() + " failed: " + exc.getMessage();
   }
}