Android Apache-上传和跟踪进度

Android Apache-上传和跟踪进度,android,Android,我正在尝试使用https+post上传图像,并跟踪其进度 StringEntity strEntity = null; int totalSize = 0; try { strEntity = new StringEntity(jsonBody.toString(), HTTP.UTF_8); strEntity.setContent

我正在尝试使用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);
我发现这里是我的类扩展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));
    }
    .....
}
然而,我发现我的writeTo方法总是被调用两次。 我不明白为什么!!请帮忙

可能与我的服务器有关吗? 谢谢你的帮助

您的writeTo方法正在调用this.wrappedEntity.writeTo,我认为这是多余的

有一次我用了这样的方法:

@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);
    }
}

所有信用卡都转到:

您可以尝试将我改为super.writeTo…,但它仍然被调用两次:…有什么建议吗?