How to get an object from HttpResponse?

I am trying to send an object from server to client.

client side:

HttpResponse response = client.execute(request);

server side:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws  IOException 
{
    PrintWriter out = response.getWriter();
    out.print(new Object());
}

How to get an object from a response?
Should I use instead:

OutputStream out = response.getOutputStream();

if so, which method is more effective?
sample code please :)
Thank you.

+3
source share
1 answer

You cannot just send Object.toString (), because it does not contain all the information about the object. Serialization is probably what you need. Take a look at this: http://java.sun.com/developer/technicalArticles/Programming/serialization/
The object you want to submit must implement Serializable. On your server, you can use something like this:

OutputStream out = response.getOutputStream();
oos = new ObjectOutputStream(out);
oos.writeObject(yourSerializableObject);

:

in = new ObjectInputStream(response.getEntity().getContent()); //Android
in = new ObjectInputStream(response.getInputStream()); //Java
ObjcetClass obj = (ObjectClass)in.readObject();
+4

All Articles