Search code examples
javaandroidmultipartentity

How do I add an Instance of a class I created to a MultipartEntity?


So I have a class Workout that I created and I would like to send it to a server using HttpPost. Is there a way I could add that class to a MultipartEntity or how should I post it?


Solution

  • First your class must implement java.io.Serializable.

            MultipartEntity e = new MultipartEntity();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(yourserializableobject);
            oos.flush();
            oos.close();
            InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(baos.toByteArray()), "o");
            e.addPart("o", isb);