On Thu, 2013-10-03 at 14:50 -0700, Phlip wrote:
> Here's an example of MultipartEntityBuilder in action:
>
> public static void postFile(String fileName) throws Exception {
> // Based on:
> http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk
>
> HttpClient client = new DefaultHttpClient();
> HttpPost post = new HttpPost(SERVER + "uploadFile");
> MultipartEntityBuilder multipartEntity =
> MultipartEntityBuilder.create();
> multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
> multipartEntity.addPart("file", new FileBody(new File(fileName)));
> post.setEntity(multipartEntity.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!
>
> How do I add a progress bar? Should I extend the FileBody class,
> override its getInputStream(), and return an input stream with an
> overridden .write that counts the bytes & sends View commands. (I'm
> inside Android, if that helps...)
>
You need to decorate the resultant HttpEntity and inside #writeTo method
wrap the original OutputStream with a ProxyOutputStream [1] subclass or
a similar implementation and have it update the UI.
Oleg
[1]
http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/output/ProxyOutputStream.html
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]