Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



16.5 More kSOAP Examples


In this section, let's check out two kSOAP applications for real-world services.


16.5.1 The Google Web Services API Demo


In the previous sections, we demonstrated how to invoke a simple Google spell check Web Service. In fact, Google offers much more than the simple service. With a proper Google ID (free registration), we can use the Google Web Services interface to search the Web and get any Web site's Google cache. I developed a complete Google API toolkit based on kSOAP. The toolkit and its MIDP UI driver are available for download from this book's Web site (see "Resources"). The Web search service has the most complex interface. The response SOAP document has three arrays of equal length: They contain the search result sites' URLs, titles, and cache sizes respectively. We can use a Java data object SearchResults to encapsulate the Google search results (see Listing 16.11).

Listing 16.11. Class SearchResults encapsulates the Google search results


public class SearchResults {
public Vector urls, titles, sizes;
public int estimatedTotalResultsCount;
public String searchTime;
public SearchResults () {
urls = new Vector ();
titles = new Vector ();
sizes = new Vector ();
estimatedTotalResultsCount = 0;
searchTime = "0.0";
}
public int getSize () {
return urls.size();
}
}

Listing 16.12 demonstrates how to send out a search query and marshal the response to a SearchResults object.

Listing 16.12. Google search



// The remote search method cannot return
// all the search results -- there could be
// tens of thousands of web sites.
//
// The "start" parameter specifies the index of
// the first entry to return.
public SearchResults search (String query,
int start) throws Exception {
// Prepare request SOAP message in a memory object
SoapObject method = new SoapObject("urn:GoogleSearch",
"doGoogleSearch");
// The free license key
method.addProperty("key", licenseKey);
// The query string to search
method.addProperty("q", query);
// The start index to return
method.addProperty("start", new Integer(start));
// Number of results to return
// from the start index
method.addProperty("maxResults", new Integer(10));
method.addProperty("filter", new Boolean(true));
method.addProperty("restrict", ");
method.addProperty("safeSearch", new Boolean(false));
method.addProperty("lr", ");
method.addProperty("ie", "latin1");
method.addProperty("oe", "latin1");
// Prepare SOAP RPC call object.
HttpTransport rpc = new HttpTransport(endPointURL, "\"\");
// Google uses 1999 SOAP standards.
ClassMap cm = new ClassMap(Soap.VER10);
rpc.setClassMap (cm);
// Conduct RPC call through HTTP and get results
SoapObject so = (SoapObject) rpc.call (method);
SearchResults sr = new SearchResults ();
sr.estimatedTotalResultsCount = ((Integer) so.getProperty(
"estimatedTotalResultsCount")).intValue ();
sr.searchTime = ((SoapPrimitive) so.getProperty(
"searchTime")).toString ();
Vector items =
(Vector) so.getProperty("resultElements");
for (int i = 0; i < items.size(); i++) {
SoapObject item = (SoapObject) items.elementAt (i);
sr.sizes.addElement (item.getProperty ("cachedSize"));
sr.urls.addElement (item.getProperty ("URL"));
sr.titles.addElement (item.getProperty ("title"));
}
return sr;
}


16.5.2 SmartPhrases


SmartPhrases is a mobile dictionary and thesaurus, smart spelling checker, and real-world usage checker. It is an award-winning application developed by Ju Long and Michael Yuan. Its source code is freely available from this book's Web site.

To be able to access dictionaries from devices, SmartPhrases includes a SOAP dictionary gateway. The gateway connects to free Internet dictionary servers using the TCP/IP-based DICT protocol. It then exposes dictionary query services through a simple SOAP interface using Apache Axis. Any mobile or desktop SOAP client can access the backend dictionary from the gateway service.

SmartPhrases utilizes Google Web Services to go beyond simple dictionaries: Google allows the user to check the usage of a phrase in real-world scenarios; it has a modern vocabulary list and makes spell suggestions for the entire phrase. The architecture of the application is shown in Figure 16.3.


Figure 16.3. The architecture of the SmartPhrases application.


For a complete demo and implementation documentation of SmartPhrases, please download its source package.


/ 204