i'm trying to serialize a HashMap on serverside(Tomcat 6.0.29) and to deserealize it on a client-side android application.
First time i've tried this i got an StreamCorruptedException, but after i created a completly new servlet with a clean doGet/doPost-Method i've managed to solve this issue, unfortunatly i get an EOFException instead.
So first i'm posting my code and then i'll explain my problem in detail:
Server-Side Code (this will be executed when doPost Or doGet is invoked):
if ("object".equals(request.getParameter("format")))
{
trace.log("Before Serializing");
ObjectOutputStream out = null;
try
{
out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(outerTable);
trace.log("after Serializing");
}
finally
{
if(out != null)
{
try
{
out.flush();
out.close();
}
catch(Exception ex)
{
trace.log("Exception has been thrown "+ex.getMessage());
}
}
}
trace.log("after closing stream");
}
else
{
PrintWriter out = response.getWriter();
out.println(outerTable.toString());
}
outerTable is a HashMap wich is created exactly before this code-block and it has always correct values.
There are no errors thrown on server-side. When i try to access the servlet in my webbrowser with parameter "?format=object" i get the whole serialization string.
Script of Android-Application (this Method executes the http request after a specific button is pressed):
private synchronized void performNetworkAccess()
{
Runnable run = new Runnable()
{
@Override
public void run()
{
System.out.println("in run()-Method");
ObjectInputStream objInput = null;
try
{
URL url = new URL(myWebsite);
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
objInput = new ObjectInputStream(in);
outerTable = (HashMap<String, HashMap<String, Object>>) objInput.readObject();
synchronized (monitor)
{
monitor.notifyAll();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (objInput != null)
{
try
{
objInput.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
};
Thread t1 = new Thread(run);
t1.start();
}
The Exception occurs immediatly after the constructor of ObjectInputStream class is invoked.
Stacktrace:
- W/System.err(1797): java.io.EOFException
- W/System.err(1797): at libcore.io.Streams.readFully(Streams.java:83)
- W/System.err(1797): at java.io.DataInputStream.readShort(DataInputStream.java:169)
- W/System.err(1797): at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2102)
- W/System.err(1797): at java.io.ObjectInputStream.(ObjectInputStream.java:372)
- W/System.err(1797): at de.tesla.jtheseuscontactsync.ContactSync$4.run(ContactSync.java:602)
- W/System.err(1797): at java.lang.Thread.run(Thread.java:856)
I've already found many occurences of this problem, but i didn't see one of them where this error occures before readObject() is invoked.
Things i've already tried:
- add out.flush() shortly after constructing ObjectOutputStream
- remove out.flush() and/or out.close() method(s)
- wrap Input-/OutputStream with BufferedInput-/-OutputStream
- retrieve stream as a file and try to deserealize the file
The odd thing is when i first write the Object to a file via FileOutputStream and access this file (on the server) from the client via Url.openStream() then ObjectOutputStream can read this object perfectly, unfortunatly this would mean a lot of trouble when multiple users trying to retrieve this object at the same time...
i hope you can help me out with this one :)