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

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

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

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

Michael Juntao Yuan

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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



16.6 What's in kSOAP v2.0?


The kSOAP v2.0 is based on the improved kXML pull parser. The pull parser could significantly improve the performance in some cases. Other important improvements of kSOAP v2.0 include the following:

Access to SOAP headers: The SoapEnvelope object makes incoming and outgoing SOAP headers available in kDOM Element arrays.

Better support for untyped SOAP servers: Some SOAP servers (notably, Microsoft .NET servers) do not bother to add the xsi:type= "xsd:string" attribute in string type elements. The kSOAP v2.0 SoapSerializationEnvelope provides a dotnet flag to turn on or off support for untyped elements.

Better structure and packaging: Compared with kSOAP v1.0, the kSOAP v2.0 internal structure is more sensible and easier to use.

- Necessary classes, such as PropertyInfo and KvmSerializable, are moved from org.kobjects to org.ksoap2 so that the kSOAP package is self-contained.

- SOAP Serialization support is now optional and contained in a separate org.ksoap2.serialization package.

- Several separate classes (e.g., ClassMap) have been integrated into the class SoapSerializationEnvelope, providing SOAP serialization support. The SoapSerializationEnvelope class extends the base class SoapEnvelope.



16.6.1 Programming for kSOAP v2.0


From the developer point of view, the object models for SoapObject, PropertyInfo and Marshal in kSOAP v2.0 are the same as those in kSOAP v1.2. However, the call model is slightly different. In kSOAP v2.0, the call arguments and return values are passed as follows.

The HttpTransport.call() method takes in a SoapEnvelope object but does not return any value.

The bodyIn data member in the SoapEnvelope object is a SoapObject containing the RPC input argument.

The marshaled result of the RPC return value is stored in SoapEnvelope.bodyOut. Depending on the return type and available Marshal, this could be a Java value object, an array or a SoapObject object.


Listing 16.13 shows how to invoke the Google Web Service to get a spell suggestion using kSOAP v2.0. Please note the use of bodyIn and bodyOut variables.

Listing 16.13. Get Google spell suggestion using kSOAP 2



public String spellCheck (String query) throws Exception {
// Prepare request SOAP message in a memory object
SoapObject method = new SoapObject("urn:GoogleSearch",
"doSpellingSuggestion");
method.addProperty("key", licenseKey);
method.addProperty("phrase", query);
// Google uses 1999 SOAP standards.
SoapEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER10);
// Set the request object
envelope.bodyOut = method;
// Prepare SOAP RPC call object.
HttpTransport rpc = new HttpTransport(endPointURL);
// Make the method call (SoapAction is not needed)
rpc.call(null, envelope);
// Retrieve marshaled return value
SoapObject ret = (SoapObject) envelope.bodyIn;
String spellSugg = (String) ret.getProperty("return");
return spellSugg;
}

The use of custom data marshal in kSOAP v2.0 is the same as that in kSOAP v1.2. Listing 16.14 shows how to get a site's Google cache using custom Base64 marshaling.

Listing 16.14. Get Google cache using kSOAP 2



import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
public class GoogleSearch {
// other methods ...
public byte [] getCache (String url) throws Exception {
// Prepare request SOAP message in a memory object
SoapObject method = new SoapObject("urn:GoogleSearch",
"doGetCachedPage");
method.addProperty("key", licenseKey);
method.addProperty("url", url);
// Google uses 1999 SOAP standards.
SoapEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER10);
// Set the request object
envelope.bodyOut = method;
// Add Base64 marshal.
// It is for both request and response data.
Marshal mb = new MarshalBase64 ();
mb.register((SoapSerializationEnvelope)envelope);
// Prepare SOAP RPC call object.
HttpTransport rpc = new HttpTransport(endPointURL);
// Make the method call (SoapAction is not needed)
rpc.call(null, envelope);
// Retrieve marshaled return value
SoapObject ret = (SoapObject) envelope.bodyIn;
byte [] cache = (byte []) ret.getProperty("return");
return cache;
}
}

Listing 16.15 shows the implementation of the kSOAP v2.0 MarshalBase64 class using the XmlPull API.

Listing 16.15. The MarshalBase64 implementation in kSOAP 2



public class MarshalBase64 implements Marshal {
static byte [] BA_WORKAROUND = new byte [0];
public static Class BYTE_ARRAY_CLASS =
BA_WORKAROUND.getClass ();
public Object readInstance (XmlPullParser parser,
String namespace, String name,
PropertyInfo expected)
throws IOException, XmlPullParserException {
Object result = Base64.decode(parser.nextText ());
return result;
}
public void writeInstance (XmlSerializer writer,
Object obj) throws IOException {
writer.text (Base64.encode ((byte[]) obj));
}
public void register (SoapSerializationEnvelope cm) {
cm.addMapping (cm.xsd, "base64Binary",
MarshalBase64.BYTE_ARRAY_CLASS, this);
cm.addMapping (SoapEnvelope.ENC, "base64",
MarshalBase64.BYTE_ARRAY_CLASS, this);
}
}


/ 204