对于UrlEn codedFormEntity Android的HTTP上传进度 [英] Android HTTP upload progress for UrlEncodedFormEntity

查看:115
本文介绍了对于UrlEn codedFormEntity Android的HTTP上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个问题讨论的方法,使进度指示可以使用的multipart / form-data的数据格式被添加到HTTP上传文件的Andr​​oid系统。建议的典型的方法是通过在<一个顶端答案缩影href="http://stackoverflow.com/questions/3213899/cant-grab-progress-on-http-post-file-upload-android">Can't抢HTTP POST文件上传(安卓)进展 - 包括完整的Apache HttpClient库的MultipartEntity类,然后包裹输入流,使用它来获取数据的一个字节数为单位,因为他们正在读<。 / P>

该方法适用于这种情况,但不幸的是,请求通过UrlEn codedFormEntity,它预计其数据传递给它的字符串,而不是InputStreams发送数据不起作用。

所以我的问题是,什么方法都可以通过这种机制来确定上传的进展如何?

解决方案

您可以覆盖任何 #writeTo 方法 HttpEntity 的实施和计数字节,因为它们会被写入到输出流。

  DefaultHttpClient的HttpClient =新DefaultHttpClient();
尝试 {
   HttpPost httppost =新HttpPost(http://www.google.com/sorry);

   MultipartEntity outentity =新MultipartEntity(){

    @覆盖
    公共无效的writeTo(最终的OutputStream outstream)抛出IOException异常{
        super.writeTo(新CoutingOutputStream(outstream));
    }

   };
   outentity.addPart(东西,新StringBody(东西));
   httppost.setEntity(outentity);

   HTT presponse RSP = httpclient.execute(httppost);
   HttpEntity inentity = rsp.getEntity();
   EntityUtils.consume(inentity);
} 最后 {
    。httpclient.getConnectionManager()关闭();
}

静态类CoutingOutputStream扩展FilterOutputStream中的{

    CoutingOutputStream(最终的OutputStream出){
        超(出);
    }

    @覆盖
    公共无效的write(int b)在抛出IOException异常{
        out.write(B);
        的System.out.println(写1字节);
    }

    @覆盖
    公共无效写入(字节[]二)抛出IOException异常{
        out.write(B);
        的System.out.println(写+ b.length个+字节);
    }

    @覆盖
    公共无效的write(byte []的B,诠释过,INT LEN)抛出IOException异常{
        out.write(B,关闭,LEN);
        的System.out.println(写+ LEN +字节);
    }

}
 

There are several questions discussing ways in which progress indication can be added to HTTP file uploads in Android using the multipart/form-data data format. The typical approach suggested is epitomized by the top answer in Can't grab progress on http POST file upload (Android) -- include the MultipartEntity class from the full Apache HTTPClient library, and then wrap the input stream it uses to get data in one that counts bytes as they're read.

This approach works for this case, but unfortunately does not work for requests sending data via UrlEncodedFormEntity, which expects its data to be passed to it in Strings rather than InputStreams.

So my question is, what approaches are available to determine progress for uploads via this mechanism?

解决方案

You can override the #writeTo method of any HttpEntity implementation and count bytes as they get written to the output stream.

DefaultHttpClient httpclient = new DefaultHttpClient();
try {
   HttpPost httppost = new HttpPost("http://www.google.com/sorry");

   MultipartEntity outentity = new MultipartEntity() {

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        super.writeTo(new CoutingOutputStream(outstream));
    }

   };
   outentity.addPart("stuff", new StringBody("Stuff"));
   httppost.setEntity(outentity);

   HttpResponse rsp = httpclient.execute(httppost);
   HttpEntity inentity = rsp.getEntity();
   EntityUtils.consume(inentity);
} finally {
    httpclient.getConnectionManager().shutdown();
}

static class CoutingOutputStream extends FilterOutputStream {

    CoutingOutputStream(final OutputStream out) {
        super(out);
    }

    @Override
    public void write(int b) throws IOException {
        out.write(b);
        System.out.println("Written 1 byte");
    }

    @Override
    public void write(byte[] b) throws IOException {
        out.write(b);
        System.out.println("Written " + b.length + " bytes");
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        out.write(b, off, len);
        System.out.println("Written " + len + " bytes");
    }

}

这篇关于对于UrlEn codedFormEntity Android的HTTP上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆