📄 bookpurchaseservice.java
字号:
// Fig. 16.23: BookPurchaseService.java.
// BookPurchaseService invokes the Book Purchase Web service.
package jws1casestudy.clients.j2me;
// import J2ME classes
import java.util.*;
// import Enhydra XML and SOAP packages
import org.ksoap.*;
import org.ksoap.transport.*;
import org.kobjects.serialization.*;
// import Deitel packages
import jws1casestudy.pricefinder.common.Customer;
public class BookPurchaseService {
// URL of Book Purchase Web service
private final static String SERVICE_URL =
"http://localhost:8080/axis/services/BookPurchase";
// namespace that Book Purchase Web service uses
private final static String NAMESPACE =
"http://schemas.xmlsoap.org/soap/envelope/";
// invoke Book Purchase Web service
public void placeOrder( PriceQuote quote, Customer customer )
{
// invoke Web service and convert result to String array
try {
// establish HTTP connection to Web-service URL
HttpTransport httpTransport =
new HttpTransport( SERVICE_URL, "" );
// create SOAP request to invoke Book Purchase service
SoapObject request =
new SoapObject( NAMESPACE, "orderBook" );
// create SoapPrimitive to represent price, because
// CLDC does not support floating-point operations
SoapPrimitive price = new SoapPrimitive(
"http://www.w3.org/2001/XMLSchema", "double",
quote.getPrice() );
// create elements quote and customer
SoapObject quoteElement = new SoapObject( "", "" );
SoapObject customerElement = new SoapObject( "", "" );
// include PriceQuote as parameter in SOAP request
quoteElement.addProperty( "price", price );
quoteElement.addProperty( "isbn", quote.getIsbn() );
quoteElement.addProperty( "storeID",
new Integer( quote.getStoreID() ) );
quoteElement.addProperty( "storeDescription",
quote.getStoreDescription() );
// include Customer as parameter in SOAP request
customerElement.addProperty( "firstName",
customer.getFirstName() );
customerElement.addProperty( "lastName",
customer.getLastName() );
customerElement.addProperty( "emailAddress",
customer.getEmailAddress() );
customerElement.addProperty( "streetAddress",
customer.getStreetAddress() );
customerElement.addProperty( "city",
customer.getCity() );
customerElement.addProperty( "state",
customer.getState() );
customerElement.addProperty( "zipCode",
customer.getZipCode() );
customerElement.addProperty( "country",
customer.getCountry() );
customerElement.addProperty( "creditCardNumber",
customer.getCreditCardNumber() );
// add elements quote and customer to root element
request.addProperty( "quote", quoteElement );
request.addProperty( "customer", customerElement );
// invoke Book Purchase Web service
httpTransport.call( request );
}
// handle exception in Web-service invocation
catch ( SoapFault fault ) {
fault.printStackTrace();
}
// handle exception in sending or receiving SOAP message
catch ( java.io.IOException ioException ) {
ioException.printStackTrace();
}
} // end method placeOrder
} // end class BookPurchaseService
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -