views:

48

answers:

1

Hi. I am developing an android application at the moment, and i'm facing a problem that i have no idea how to solve. My application communicates with a Tomcat Server, and i'm using ObjectOutputStream to send a Document object to my application from my servlet. There fore, i'm importing org.w3c.dom to my project. The problem i'm facing is, that I can't read the Document object with ObjectInputStream on my android device. I'm getting the following exception.

com.sun.org.apache.xerces.internal.dom: ClassNotFoundException

on the lines:

ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
Document doc = (Document) ois.readObject();     
ois.close();

I can send and recieve any object i want without any problem, but when I try to send an object refered to the com.w3c.dom package I get this exception. Can anybody help?

A: 

The Sun JVM implementation of org.w3c.Document uses a class situated in a package that is included only in the Sun JVM (as the "com.sun.something" package name hints.)

When the Android Dalvik VM tries to deserialize the Object in readObject, it finds a reference to that internal, Sun JVM specific, class - because the Dalvik VM does not have a reference to that class (Dalvik's org.w3c.Document implementation is different), an exception is thrown. (More generally, Java Object serialization/deserialization is supposed to work only if both serializer and deserializer share the same class implementation. I suspect that most of your deserialization routines would fail if your instance of Tomcat was not running on a Sun JVM.)

Seeing that you are sending org.w3c.Document objects, and that this class natively represents an XML document, you could solve the problem by sending an XML stream rather than an Object stream.

jhominal
Thank you very much for your quick response. Though i think, I got the point from your answer I have decided to use another solution to my problem. By sending a string object containing the entire xml document from my server, I can compose a new Document object in my android application by using a DocumentBuilder with a StringReader as InputSource.
Nahpets