android - How to post byte array using Retrofit 2 -
i new use retrofit , want send byte array of file server failed response server, , post file using volley , httpurlconnection both. please me, code snippet .
public class apiclientpost { private static final string base_url = "http://base.url/api/"; private static retrofit retrofit = null; public static retrofit getclient(){ if(retrofit == null){ retrofit = new retrofit.builder() .baseurl(base_url) .addconverterfactory(gsonconverterfactory.create()) .build(); } return retrofit; } } public interface apiinterface { @multipart @headers({ "content-type: multipart/form-data" }) @post("eclaims/uploadfiles") call<jsonelement> uploadfiles(@part multipartbody.part body); } fileinputstream fin = null; try { fin = new fileinputstream(file); bufferedinputstream bis = new bufferedinputstream(fin); datainputstream dis = new datainputstream(bis); filecontent = tobytearray(dis); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } mediatype mediatype = mediatype.parse("video/mp4"); requestbody requestfile = requestbody.create(mediatype, file ); multipartbody.part body = multipartbody.part.createformdata("", file.getname(), requestfile); apiinterface apiinterface = apiclientpost.getclient().create(apiinterface.class); call<jsonelement> uploadfile = apiinterface.uploadfiles(body); uploadfile.enqueue(new callback<jsonelement>() { @override public void onresponse(call<jsonelement> call, response<jsonelement> response) { if (response.issuccessful()) { jsonelement mainresponse = response.body(); log.d("response ===", mainresponse.tostring()); } else { log.e("response ===", "failed"); } } @override public void onfailure(call<jsonelement> call, throwable t) { log.e("failed ===", t.getmessage()); } });
sorry unable give url. have sensitive data. failed response server when convert image or video file byte array , send byte array server.
Comments
Post a Comment