📄 bookpriceservice.java
字号:
// Fig. 16.20: BookPriceService.java.
// Class BookPriceService invokes the Best Book Price 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.*;
public class BookPriceService {
// URL of Best Book Price Web service
private final static String SERVICE_URL =
"http://localhost:8080/axis/services/BestBookPrice";
// namespace that Best Book Price Web service uses
private final static String NAMESPACE =
"http://common.pricefinder.jws1casestudy.PriceQuote";
// invoke Best Book Price Web service
public PriceQuote getBestPrice( String isbn )
{
// invoke Web service and convert result to PriceQuote
try {
// establish HTTP connection to Web-service URL
HttpTransport httpTransport =
new HttpTransport( SERVICE_URL, "" );
// create SOAP request to invoke Best Book Price service
SoapObject request =
new SoapObject( NAMESPACE, "getBestPrice" );
// include ISBN as parameter in SOAP request
request.addProperty( "isbn", isbn );
// invoke Best Book Price Web service
SoapObject response = ( SoapObject )
httpTransport.call( request );
// obtain price from response
SoapPrimitive price =
( SoapPrimitive ) response.getProperty( "price" );
// obtain ISBN from response
String ISBN = ( String ) response.getProperty( "isbn" );
// obtain storeID from response
Integer storeID =
( Integer ) response.getProperty( "storeID" );
// obtain storeDescription from response
String storeDescription =
( String ) response.getProperty( "storeDescription" );
// create PriceQuote from response values
PriceQuote priceQuote = new PriceQuote(
price.toString(), ISBN, storeID.intValue(),
storeDescription );
return priceQuote;
}
// handle exception in Web-service invocation
catch ( SoapFault fault ) {
fault.printStackTrace();
return null;
}
// handle exception in sending or receiving SOAP message
catch ( java.io.IOException ioException ) {
ioException.printStackTrace();
return null;
}
// handle exception in PriceQuote cast operation
catch ( ClassCastException exception ) {
exception.printStackTrace();
return null;
}
} // end method getBestPrice
} // end class BookPriceService
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -