Custom Object Serialization in Android?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Custom Object Serialization in Android?

Postby dsukhram » Mon Oct 12, 2009 11:30 pm

I am writing an android client app which sends a Hashtable to a remote servlet and recieves an ArrayList consisting of a custom Object which is serializable. This ran fine when I was just receiving the ArrayList from a java console app. So I know my objects are being serialized and deserialized properly. But when I port the same client code over to android I get an error when trying to read the ArrayList of my objects. Is there any differences in the way Android does serialization? I can't seem to find any documentation that says it does. The error I receive is:

java.lang.IndexOutOfBoundsException: Invalid location

at java.util.ArrayList.get(ArrayList.java:350)

============ My Android Client app is as follows=================================

public class TTCPointService {
private Logger logger;
private FileHandler handler;

public TTCPointService(){

try
{
logger = Logger.getLogger(TTCPointService.class.getName());
handler = new FileHandler("ttcservice.log");
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
}
catch(IOException e)
{
logger.log(Level.SEVERE,e.toString());
}
}

public ArrayList<Stop> ConnectToServlet()
{
ArrayList<Stop> list = new ArrayList<Stop>();

Hashtable<String, String>obj = new Hashtable<String,String>();
obj.put("lat", "12.00");
obj.put("log", "-23.00");

try
{
URL servletURL = new URL("http://10.0.2.2:8080/GTFSProcessor/TTCPoints");
HttpURLConnection servletConnection = (HttpURLConnection)servletURL.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);

servletConnection.setRequestProperty("Content-type","application/x-java-serialized-object");
servletConnection.setRequestMethod("POST");
logger.log(Level.INFO,"Connecting to Servlet");

//Send object to servlet
OutputStream os = servletConnection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);

oos.flush();
oos.close();
logger.log(Level.INFO,"Hashtable sent to Servlet");

//Read from servlet
InputStream is = servletConnection.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);


list = (ArrayList<Stop>)ois.readObject();
logger.log(Level.INFO,"Reading Object from Servlet");

os.close();

}
catch(IOException e)
{
logger.log(Level.SEVERE,e.toString());
}
catch(ClassNotFoundException e)
{
logger.log(Level.SEVERE,e.toString());
}
return(list);
}


}
=============The code that raises the error is===================================

ArrayList<Stop> list = new ArrayList<Stop>();
TTCPointService service = new TTCPointService();
list = (ArrayList<Stop>)service.ConnectToServlet();

Toast.makeText(mapView.getContext(), "list contains: "+list.get(0).getName(), Toast.LENGTH_SHORT).show();
Last edited by dsukhram on Tue Oct 13, 2009 8:43 pm, edited 1 time in total.
dsukhram
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 06, 2009 10:49 pm

Top

Postby padde » Tue Oct 13, 2009 1:22 am

Hmm.. i dont know exactly what your servlet is returning.. but i guess you are aware of the fakt
you sending a hashtable to the servlet and try to receive a arraylist in return. Just to cover the case
that you stared so long at the code that you overlooked something trivial like that.. i have been there
before so often by myself :D

If this is not what causing the error.. what i assume.. maybe this link could lead you to a solution:

http://developerlife.com/tutorials/?p=288


Please let me know if you work it out.. because i need almost the exact same thing in a day or two :lol:
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby dsukhram » Thu Oct 15, 2009 11:33 pm

I solved my problem using protobuf.


padde wrote:Hmm.. i dont know exactly what your servlet is returning.. but i guess you are aware of the fakt
you sending a hashtable to the servlet and try to receive a arraylist in return. Just to cover the case
that you stared so long at the code that you overlooked something trivial like that.. i have been there
before so often by myself :D

If this is not what causing the error.. what i assume.. maybe this link could lead you to a solution:

http://developerlife.com/tutorials/?p=288


Please let me know if you work it out.. because i need almost the exact same thing in a day or two :lol:
dsukhram
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 06, 2009 10:49 pm

Postby padde » Fri Oct 16, 2009 12:12 am

I heard of it.. but isnt the lib for android quite big? And i also red that de-/marshaling is slower than
native java serialization.

I found a interesting JSON vs. Protobuf comparison here:
http://www.4feets.com/2009/08/serializing-data-json-vs-protocol-buffers/

Have you gathered some Java Serialization vs. Protobuf comparison data you could
share with us by any chance? :D

--- Edit ---
found some interesting results make sure to check out the sequel post (link in the last comment)
http://www.eishay.com/2008/11/serialization-protobuf-vs-thrift-vs.html

I dug a bit deeper and decided to give protobuf a chance in my current app.
Because it took me some time to compile the protobuf lite version (which offers all i need)
i thought i add the compiled lib for you guys :wink:
Attachments
protobuf-java.2.2.0rc3-lite.rar
(49.1 KiB) Downloaded 42 times
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby dsukhram » Fri Oct 16, 2009 2:00 pm

I wasn't aware protobuf had a 'lite' implementation. I'll have to look into it. thanks for the link. Here is a really good benchmark comparison: http://code.google.com/p/thrift-protobu ... nchmarking
dsukhram
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 06, 2009 10:49 pm

Postby padde » Fri Oct 16, 2009 3:52 pm

This benchmark is really nice and gives a good overview of alternative libs.
I kinda changed my mind an will give kryo a chance.. it performs in almost any
cases similar to protobuf, it comes out of the box as a small lib (~58kb) and it
has compression support. But the real kicker is the ease of use.. no need for
pre-compiled .proto files or something like that.

FYI the protobuf lite is the same as protobuf (so fully compatible with "normal" protobuf)
just some rather rare used features like reflection and descriptors got kicked out to
reduce the lib size.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 1 guest