Java built-in serialization has many benefits, but for efficiency you
may want to consider something else, such as protobuf.



Yusuf Saib
Android
·T· · ·Mobile· stick together
The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.



On Oct 13, 12:41 pm, Dan Sherman <[email protected]> wrote:
> Not sure as to an answer to your question (I'm not that familiar with the
> internals), however, as a work-around you could probably do something like:
>
> MyObject arr[] = new MyObject[list.size()];
> serialize(arr);
> send(arr);
>
> -----
> recv(arr)
> list.addAll(arr);
>
> Obviously abbreviated, and doesn't quite answer the question, but there's
> definitely ways to get the data from A to B differently...
>
> - Dan
>
>
>
> On Tue, Oct 13, 2009 at 3:32 PM, dsukhram <[email protected]> wrote:
>
> > I can return an ArrayList of Strings just not an ArrayList of my Stop
> > object. And since this whole process works fine when I take android
> > out of the equation..this leads me to believe that serialization is
> > handled slightly differently in android.  Anybody?
>
> > On Oct 12, 6:28 pm, dsukhram <[email protected]> wrote:
> > > 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 that is sent back from the servlet.
>
> > > 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://192.168.0.69: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();
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to