how to perform file upload using Volley in android? -
i want upload files(images,documents etc) android application server.i'm using volley library network calls in application.
i'm using following code upload files not performing operation.(showing volley timeout error finally)
public class multipartrequest extends request<string> { private multipartentity entity = new multipartentity(); private static final string file_part_name = "file"; private final response.listener<string> mlistener; private final file mfilepart; private string mstringpart,accesstoken; public multipartrequest(string url, response.errorlistener errorlistener, response.listener<string> listener, file file,string accesstoken) { super(method.post, url, errorlistener); mlistener = listener; mfilepart = file; this.accesstoken = accesstoken; buildmultipartentity(); } private void buildmultipartentity() { entity.addpart(file_part_name, new filebody(mfilepart)); try { entity.addpart("content-disposition", new stringbody("form-data")); entity.addpart("dir_path", new stringbody("izezoje3")); } catch (unsupportedencodingexception e) { volleylog.e("unsupportedencodingexception"); } } @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("content-type", "multipart/form-data"); headers.put("_pkta",accesstoken); return headers; } @override public string getbodycontenttype() { return entity.getcontenttype().getvalue(); } @override public byte[] getbody() throws authfailureerror { bytearrayoutputstream bos = new bytearrayoutputstream(); try { entity.writeto(bos); } catch (ioexception e) { volleylog.e("ioexception writing bytearrayoutputstream"); } return bos.tobytearray(); } @override protected response<string> parsenetworkresponse(networkresponse response) { return response.success("uploaded", getcacheentry()); } @override protected void deliverresponse(string response) { mlistener.onresponse(response); } }
please me on how implement file upload in android(either volley or other)
try
public multipartrequest(string url, string filepath, response.listener<string> listener, response.errorlistener errorlistener) { super(method.post, url, errorlistener); entity = new multipartentity(httpmultipartmode.browser_compatible); file = new file(filepath); mlistener = listener; buildmultipartentity(); }
also, in case didn't override getheaders, passed other values addpart, e.g:
entity.addpart("file", new filebody(file, "image/jpeg")); // image entity.addpart("id", new stringbody(userid));
hope helps.
Comments
Post a Comment