the short version is org.apache...MultipartEntity deprecated, and its update MultipartEntityBuilder is underrepresented in our online forums. Let me fix it. How to register a callback, so my application (Android) can display a progress bar when it loads?
long version . Here is the "missing simple example" MultipartEntityBuilder :
public static void postFile(String fileName) throws Exception { // Based on: https://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(SERVER + "uploadFile"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("file", new FileBody(new File(fileName))); builder.addTextBody("userName", userName); builder.addTextBody("password", password); builder.addTextBody("macAddress", macAddress); post.setEntity(builder.build()); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); // response.getStatusLine(); // CONSIDER Detect server complaints entity.consumeContent(); client.getConnectionManager().shutdown(); } // FIXME Hook up a progress bar!
We need to fix this FIXME . (An additional benefit would be to interrupt the download.) But (please correct me, am I not mistaken or not), all online examples seem short.
This http://pastebin.com/M0uNZ6SB , for example, downloads the file as a "binary / octet stream"; not "multipart / form-data". I need real fields.
In this example, Downloading a Java file (with a progress bar) shows how to override *Entity or *Stream . Maybe I can tell MultipartEntityBuilder to .create() overridden object that measures its load level?
So, if I want to redefine something and replace the built-in stream with a counting stream that sends a signal for every 1000 bytes, maybe I can expand the FileBody part and redefine it with getInputStream and / or writeTo .
But when I try class ProgressiveFileBody extends FileBody {...} , I get the infamous java.lang.NoClassDefFoundError .
So while I look through the .jar files, looking for the missing Def, can someone check my math and maybe point to a simpler fix that I missed?
Phlip Sep 23 '13 at 16:27 2013-09-23 16:27
source share