android - How and why to send a MultiPartEntity (best chunked) with OKHttp? -



android - How and why to send a MultiPartEntity (best chunked) with OKHttp? -

i know it's improve utilize okhttp based on httpurlconnection, dont know why.

why improve utilize okhttp instead of implemented httpurlconnection?

and.. how possible send mixed data. post vars , files. best if there multithreaded possible.

here's illustration of client using httpurlconnection. how in okhttp?

dataclass , datapackage modals holding files , info sending.

the client if self called factorymanager (lazyloading) using

public static defaulthttppostclient getenterprisehttpmanager() { if (defaulthttppostclient == null) { defaulthttppostclient = new defaulthttppostclient(); } homecoming defaulthttppostclient; }

the constructor of httpclient request it's settings everytime when called.

public static httpconnectionsettings getdefaulthttpconnectionsettings() throws malformedurlexception { httpconnectionsettings httpconnectionsettings = new httpconnectionsettings(); httpconnectionsettings.setpost(true); httpconnectionsettings.setmultithreaded(true); httpconnectionsettings.seturl(new url(myurl); homecoming httpconnectionsettings; } public httpresult executerequest() { httpurlconnection connection = null; outputstream out = null; inputstream in = null; httpresult httpresult = new httpresult(); seek { connection = mfactory.getokhttpclient().open(httpconnectionsettings.geturl()); connection.setconnecttimeout(300000); connection.setusecaches(false); connection.setdooutput(true); connection.setdoinput(true); connection.setrequestmethod((httpconnectionsettings.ispost()) ? "post" : "get"); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("cache-control", "no-cache"); connection.setreadtimeout(300000); connection.setdefaultusecaches(true); connection.setrequestproperty("accept-charset", "utf-8"); if (httpconnectionsettings.ispost()) { connection.setrequestproperty("content-type", "multipart/form-data, boundary=xxxxxxxxxxyy"); } out = connection.getoutputstream(); multipartentity multipartentity = new multipartentity(); (object amdata : dataclass.getdata().entryset()) { map.entry mapentry = (map.entry) amdata; string keyvalue = (string) mapentry.getkey(); string value = (string) mapentry.getvalue(); if (value != null) { multipartentity.addpart(new stringpart(keyvalue, value)); } else { log.d(this.getclass().getsimplename(), "value null key " + keyvalue); } } (datapackage datapackage : dataclass.getdatapackages()) { if (datapackage.getbfile() != null) { multipartentity.addpart(new bytepart("upfile", datapackage.getbfile(), datapackage.getfname())); } else if (!datapackage.getsfile().equals("")) { multipartentity.addpart(new filepart(new file(datapackage.getsfile()), datapackage.getfname(), datapackage.getmimetype())); } else if (datapackage.getmfile() != null) { multipartentity.addpart(new filepart(datapackage.getmfile(), datapackage.getfname(), datapackage.getmimetype())); } if (datapackage.getmimetype() != null) { multipartentity.addpart(new stringpart("mime", datapackage.getmimetype())); } if (datapackage.getfilesize() != 0) { multipartentity.addpart(new stringpart("filesize_orig", string.valueof(datapackage.getfilesize()))); } if (datapackage.getfilepath() != null) { multipartentity.addpart(new stringpart("filepath_orig", datapackage.getfilepath())); } if (datapackage.getfname() != null) { multipartentity.addpart(new stringpart("filename_orig", datapackage.getfname())); } if (datapackage.getlastmodified() != 0) { multipartentity.addpart(new stringpart("lastmodified_orig", string.valueof(datapackage.getlastmodified()))); } if (datapackage.getadded() != 0) { multipartentity.addpart(new stringpart("added_orig", string.valueof(datapackage.getadded()))); } } multipartentity.writeto(out); if (connection.getresponsecode() != httpurlconnection.http_ok) { log.e("httpresult", "response: " + connection.getresponsecode() + " " + connection.getresponsemessage() + " content:" + connection.geturl()); httpresult.setsuccess(false); } else { log.d("httpresult", "response: " + connection.getresponsecode() + " " + connection.getresponsemessage() + " content:" + connection.geturl()); httpresult.setsuccess(true); httpresult.setinputstream(connection.getinputstream()); } } grab (exception e) { e.printstacktrace(); } { seek { (datapackage datapackage : dataclass.getdatapackages()) { if (datapackage.isdeleteafter()) { new file(datapackage.getfilepath()).delete(); } } } grab (exception e) { e.printstacktrace(); } if (out != null) { seek { out.close(); } grab (exception e) { } } if (in != null) { seek { in.close(); } grab (exception e) { } } homecoming httpresult; } }

does create sense switiching okhttp? i've lots of requests. there 100 threads open (each thread executor it's own httprequest).

why should utilize okhttp? far.

the easiest thing in situation utilize okurlfactory okhttp-urlconnection module. can utilize okhttp 2 without hassle of changing much.

you'll need switch ohttp's new api if want allow okhttp take care of dispatching requests. there's no obligation in situation.

android multithreading http okhttp

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -