简体   繁体   English

Android Apache-上传并跟踪进度

[英]Android Apache - Upload and track progress

I am trying to upload an image using https + post, and track its progress. 我正在尝试使用https + post上传图像,并跟踪其进度。

            StringEntity strEntity = null;
            int totalSize = 0;

            try 
            {
                strEntity = new StringEntity(jsonBody.toString(), HTTP.UTF_8);
                strEntity.setContentEncoding(HTTP.UTF_8);
                strEntity.setContentType("application/json");
                totalSize = jsonBody.toString().getBytes().length;
            } 
            catch (UnsupportedEncodingException e) 
            {
                e.printStackTrace();
            }

            ProgressHttpEntityWrapper httpEntity = new ProgressHttpEntityWrapper(strEntity, progressCallback, totalSize);
            httpPost.setEntity(httpEntity);

I found Here is my class extends HttpEntityWrapper 我发现这是我的类扩展HttpEntityWrapper

public class ProgressHttpEntityWrapper extends HttpEntityWrapper 
{
    private final ProgressCallback progressCallback;
    private final long fileSize;

    public ProgressHttpEntityWrapper(final HttpEntity entity, final ProgressCallback progressCallback, int fileSize) 
    {
        super(entity);
        this.progressCallback = progressCallback;
        this.fileSize = fileSize;

        Log.e("AsyncUploadData", "Constructor");
    } 

    @Override
    public void writeTo(final OutputStream out) throws IOException 
    {
        Log.e("AsyncUploadData", "writeTo: " +getContentLength());

        super.writeTo(out instanceof ProgressFilterOutputStream ? out
                : new ProgressFilterOutputStream(out, this.progressCallback, this.fileSize));
    }
    .....
}

However, I found my "writeTo" method is always being called twice. 但是,我发现我的“ writeTo”方法始终被调用两次。 I couldn't figure out why!! 我不知道为什么! Please help! 请帮忙!

Is it possible to have something to do with my server? 可能与我的服务器有关吗? Thanks for any help!! 谢谢你的帮助!!

Your writeTo() method is calling this.wrappedEntity.writeTo() which I think is redundant. 您的writeTo()方法正在调用this.wrappedEntity.writeTo() ,我认为这是多余的。

I used something like this one time: 我曾经用过这样的事情:

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    super.writeTo(new CountingOutputStream(outstream, this.listener));
}

public static interface ProgressListener {
    void transferred(long num);
}

public static class CountingOutputStream extends FilterOutputStream {

    private final ProgressListener listener;
    private long transferred;

    public CountingOutputStream(final OutputStream out, final ProgressListener listener) {
        super(out);
        this.listener = listener;
        this.transferred = 0;
    }

    public void write(byte[] b, int off, int len) throws IOException {
        out.write(b, off, len);
        this.transferred += len;
        this.listener.transferred(this.transferred);
    }

    public void write(int b) throws IOException {
        out.write(b);
        this.transferred++;
        this.listener.transferred(this.transferred);
    }
}

All credits goes to: http://toolongdidntread.com/android/android-multipart-post-with-progress-bar/ 所有学分归功于: http : //toolongdidntread.com/android/android-multipart-post-with-progress-bar/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM