android - Image upload with MultipartEntityBuilder -
i have following class, don't know, how resolve contenttype
. why cannot used writeto()
method created entity? cannot write byteoutputstream
.
public class imageuploadrequest<t> extends request<t> { private static final string file_part_name = "file"; private multipartentitybuilder mbuilder = multipartentitybuilder.create(); private final response.listener<t> mlistener; private final file mimagetoupload; protected map<string, string> headers; public imageuploadrequest(string uploadurl, response.errorlistener errorlistener, response.listener<t> listener, file imagefiletoupload){ super(method.post, uploadurl, errorlistener); mlistener = listener; mimagetoupload = imagefiletoupload; //call helper method build multipart entity buildmultipartentity(); } @override public map<string, string> getheaders() throws authfailureerror { map<string, string> headers = super.getheaders(); if (headers == null || headers.equals(collections.emptymap())) { headers = new hashmap<string, string>(); } headers.put("accept", "application/json"); return headers; } private void buildmultipartentity(){ mbuilder.addbinarybody(file_part_name, mimagetoupload, contenttype.create("image/jpeg"), mimagetoupload.getname()); mbuilder.setmode(httpmultipartmode.browser_compatible); mbuilder.setlaxmode().setboundary("xx").setcharset(charset.forname("utf-8")); } @override public string getbodycontenttype(){ string contenttypeheader = mbuilder.build().getcontenttype().getvalue(); return contenttypeheader; } @override public byte[] getbody() throws authfailureerror{ bytearrayoutputstream bos = new bytearrayoutputstream(); try { mbuilder.build().writeto(bos); } catch (ioexception e) { volleylog.e("ioexception writing bytearrayoutputstream bos, building multipart request."); } return bos.tobytearray(); } @override protected response<t> parsenetworkresponse(networkresponse response) { t result = null; return response.success(result, httpheaderparser.parsecacheheaders(response)); } @override protected void deliverresponse(t response) { mlistener.onresponse(response); } }
Comments
Post a Comment