简体   繁体   English

检查上传进度(适用于Android的Google GMail API)

[英]Check progress for Upload (Google GMail API for Android)

I'm getting frustrated trying to get information about the progress of my asynctask while inserting a mail through Gmail Api for Android. 在通过Android版Gmail Api插入邮件时,尝试获取有关asynctask进度信息的过程让我感到沮丧。

I'm trying to use the MediaHttpUploaderProgressListener but the problem is that i just get null out of the getMediaHttpUploader() method of the insert. 我正在尝试使用MediaHttpUploaderProgressListener,但问题是我只是从插入的getMediaHttpUploader()方法中获取了空值。

I think i'm missing something big in the middle to get the expected response out of the getMediaHttpUploader... 我想我在中间缺少一些大东西来从getMediaHttpUploader中获得预期的响应...

This is my code: 这是我的代码:

private class TareaGestionEnvioMails extends AsyncTask<Void, Void, Message> {
    @Override
    protected Message doInBackground(Void... params) {
        try {
            if (mensaje != null) {
                String user = "me";

                Gmail.Users.Messages.Insert insert = servicioGmail.users().messages().insert(user, mensaje);
                MediaHttpUploader uploader = insert.getMediaHttpUploader();
                //getMediaHttpUploader returns me null value :(
                uploader.setDirectUploadEnabled(false);
                uploader.setChunkSize(1024*256);
                uploader.setProgressListener(new FileUploadProgressListener());

                mensaje = insert.execute();
            }
            return mensaje;
        } catch (Exception e) {
            mLastError = e;
            cancel(true);
            return null;
        }
    }
...

    private class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

    public FileUploadProgressListener() {
    }

    @Override
    public void progressChanged(MediaHttpUploader mediaHttpUploader) throws IOException {
        if (mediaHttpUploader == null) return;
        switch (mediaHttpUploader.getUploadState()) {
            case INITIATION_STARTED:
                pantallaPrincipal.onProgresoEnviarMail(0.0, "INITIATION_STARTED");
                break;
            case INITIATION_COMPLETE:
                pantallaPrincipal.onProgresoEnviarMail(0.0, "INITIATION_COMPLETE");
                break;
            case MEDIA_IN_PROGRESS:
                double percent = mediaHttpUploader.getProgress() * 100;
                pantallaPrincipal.onProgresoEnviarMail(percent, "MEDIA_IN_PROGRESS");
                break;
            case MEDIA_COMPLETE:
                pantallaPrincipal.onProgresoEnviarMail(100.0, "MEDIA_COMPLETE");
        }
    }
}

Thanks Everyone! 感谢大家! This is my first time posting on Stackoverflow! 这是我第一次在Stackoverflow上发帖!

Solved!!!! 解决了!!!!

You have to use the three parameter version of the insert() method. 您必须使用insert()方法的三个参数版本。 I've filled the third parameter with my MimeMessage this way: 我已经用我的MimeMessage填充了第三个参数:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
myMimeMessage.writeTo(baos);
AbstractInputStreamContent mediaContent = new ByteArrayContent("message/rfc822", baos.toByteArray());
Gmail.Users.Messages.Insert insert = servicioGmail.users().messages().insert(user, null, mediaContent);

There are two things you have to be aware of: 您必须注意两件事:

1) If you keep using the " Message " and the " AbstractInputStreamContent " parameters both at the same time, you will be happy with little messages... But if you have to upload a message bigger than around 1mb, you will get a horrible 400 Bad_Request exception with a "Request too large" error. 1)如果同时使用“ Message ”和“ AbstractInputStreamContent ”两个参数,您将对小消息感到满意...但是如果您必须上载大于1mb的消息,您将感到非常恐怖。 400 Bad_Request异常,出现“请求太大”错误。 So, leave the Message parameter null. 因此,将Message参数保留为空。

2) Leaving the "Message" parameter null, you can't set the Label of the message... so you will have to fill it later using users().messages(). 2)将“ Message”参数保留为空,就无法设置消息的标签 ...,因此稍后必须使用users()。messages()来填充它。 modify method. 修改方法。

If anyone have a nicer workaround to insert a big mail and it's label at the same time, and having a feedback of the progress, please comment! 如果有人有更好的解决方法来同时插入一个大邮件及其标签,并且对进度有反馈,请发表评论! Thanks! 谢谢!

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

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