json - How to write type adapter for GSON to send stream? -
one of fields of class filename. serialization i'm going write gson type adapter (implements jsonserializer<myclass>
) should send file stream.
the problem don't want read file data (stream) , hold string in memory memory size limited (it's mobile device) , have send fields (filename
f.e. below), json should like:
data: { filename:"filename.png" filedata:"(base64 file data stream here)" }
what best way send file data in network field in case?
ps. network sending done apache http client if helps
it seems it's not architectural solution mix json , large binary data within 1 request body. 1 can use http multipart instead:
httppost request = new httppost(url); multipartentity multipartentity = new multipartentity(); request.setentity(multipartentity); // body try { multipartentity.addpart("json", new stringbody(body, "application/json", charset.forname("utf-8"))); } catch (unsupportedencodingexception e) { throw new resourceloadingexception(e); } // files (int i=0; i<filespaths.size(); i++) { string eachfilepath = filespaths.get(i); file file = new file(eachfilepath); multipartentity.addpart("file" + string.valueof(i), new filebody(file)); }
what mobile devices android not support multipart bodies can add support (if using maven or gradle):
<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpmime</artifactid> <version>4.2.5</version> </dependency>
Comments
Post a Comment