Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上使用javaMail从Gmail检索附件的慢速(Rx)_Android_Gmail_Jakarta Mail - Fatal编程技术网

在Android上使用javaMail从Gmail检索附件的慢速(Rx)

在Android上使用javaMail从Gmail检索附件的慢速(Rx),android,gmail,jakarta-mail,Android,Gmail,Jakarta Mail,我正在使用JavaMail库访问Gmail,下载压缩文件附件(1MB)时速度较慢(Rx): 如您所见,获取附件并将其写入SD大约需要20秒 这是我的密码: public class MailFile { InputStream is; String fileName; public MailFile( InputStream is, String fileName) { this.is = is; this.fileName = fileName; } public Inp

我正在使用JavaMail库访问Gmail,下载压缩文件附件(1MB)时速度较慢(Rx):

如您所见,获取附件并将其写入SD大约需要20秒

这是我的密码:

public class MailFile {
InputStream is;
String fileName;

public MailFile( InputStream is, String fileName) {
    this.is = is;
    this.fileName = fileName;
}

public InputStream getIs() {
    return is;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}}
正在检索邮件:

private List<MailFile> getAttachments(Message temp) throws Exception {
    List<MailFile> attachments = new ArrayList<MailFile>();
    Multipart multipart = (Multipart) temp.getContent();

    for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        if (bodyPart.getFileName()!=null && (bodyPart.getFileName().contains(ZIP_EXT) || bodyPart.getFileName().contains(XLSX_EXT)))
            attachments.add(new MailFile(bodyPart.getInputStream(), bodyPart.getFileName()));
    }

    return attachments;
}

protected String doInBackground(String... params) {
    try {
        SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(activity.getBaseContext());

        Properties props = new Properties();
        //IMAPS protocol
        props.setProperty("mail.store.protocol", "imaps");
        //Set host address
        props.setProperty("mail.imaps.host", "imaps.gmail.com");
        //Set specified port
        props.setProperty("mail.imaps.port", "993");
        //Using SSL
        props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imaps.socketFactory.fallback", "false");

        props.setProperty("mail.imaps.partialfetch", "false"); //this line solved my problem


        Session imapSession = Session.getInstance(props);
        Store store = imapSession.getStore("imaps");
        store.connect("imap.gmail.com", SP.getString("incomingMail", activity.getString(R.string.confIncomingDefaultValue)), SP.getString("passwordMail", activity.getString(R.string.confIncomingDefaultValue)));
        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_WRITE);

        Message msgArr[] = inbox.search(this.searchTerm);
        //Message msg = inbox.getMessage(inbox.getMessageCount());
        if (msgArr.length > 0) {
            Message msg = msgArr[msgArr.length - 1]; //only last message
            Object content = msg.getContent();

            if (content instanceof Multipart) //si és un correu amb attach
            {
                Multipart mp = (Multipart) content;
                for (MailFile f : getAttachments(msg)) {
                    if (f.getFileName().contains(XLSX_EXT)) {
                        new XLSXReader(f.getIs());
                        // msg.setFlag(Flags.Flag.DELETED, true);
                    } else if (f.getFileName().contains(ZIP_EXT)) {

                        long startTime = System.currentTimeMillis();

                        writeToFile(f.getIs(),new FileOutputStream(f.getFileName()));

                        long duration = System.currentTimeMillis() - startTime;
                        Log.d("Time", "Time " + duration + "ms " + duration / 1000.0f + " sec");


                        ZipperFolders.unzipFile(unzipPath, f.getFileName());
                        loadZipObjects(f.getFileName());
                    }
                }
                doToast(activity.getString(R.string.messagesReceivedOK));
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((PrincipalActivity) activity).setDate(null, false);
                    }
                });
            } else
                doToast(activity.getString(R.string.messagesReceivedKO));
        } else
            doToast(activity.getString(R.string.messagesNoneMessages, SP.getString("incomingMail", activity.getString(R.string.confIncomingDefaultValue))));

        inbox.close(true);
        store.close();

    } catch (Exception e) {
        e.printStackTrace();
        doToast(activity.getString(R.string.messagesConnectionKO));
    }
    return activity.getString(R.string.messagesReceived);

}
private List getAttachments(消息临时)引发异常{
列表附件=新的ArrayList();
Multipart Multipart=(Multipart)temp.getContent();
对于(int i=0;i0){
Message msg=msgArr[msgArr.length-1];//仅限最后一条消息
对象内容=msg.getContent();
if(content instanceof Multipart)//siés un correu amb attach
{
多部分mp=(多部分)内容;
用于(邮件文件f:getAttachments(msg)){
如果(f.getFileName()包含(XLSX_EXT)){
新的XLSXReader(f.getIs());
//msg.setFlag(Flags.Flag.DELETED,true);
}else if(f.getFileName().contains(ZIP_EXT)){
long startTime=System.currentTimeMillis();
writeToFile(f.getIs(),newfileoutputstream(f.getFileName());
长持续时间=System.currentTimeMillis()-startTime;
日志d(“时间”、“时间”+持续时间+“毫秒”+持续时间/1000.0f+“秒”);
unzippfile(unzipPath,f.getFileName());
loadZipObjects(f.getFileName());
}
}
doToast(activity.getString(R.string.messagesReceivedOK));
activity.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
((PrincipalActivity)活动).setDate(null,false);
}
});
}否则
doToast(activity.getString(R.string.messagesReceivedKO));
}否则
doToast(activity.getString(R.string.messagesNoneMessages,SP.getString(“incomingMail”,activity.getString(R.string.confIncomingDefaultValue)));
收件箱。关闭(true);
store.close();
}捕获(例外e){
e、 printStackTrace();
doToast(activity.getString(R.string.messagesConnectionKO));
}
返回activity.getString(R.string.messagesReceived);
}
我尝试过使用mimessage,但结果是一样的。如果我不在Gmail应用程序中加载相同的文件,需要2秒钟!。发生了什么事?Gmail限制了我的带宽吗?我做错什么了吗?谢谢

编辑:我已经解决了添加
props.setProperty(“mail.imaps.partialfetch”、“false”)时遇到的问题在声明会话对象之前

这是结果Rx:


从20秒开始。到2秒。难以置信

如果您读取数据而不在任何地方写入数据,需要多长时间?如果在没有从消息读取数据的情况下将数据写入文件,需要多长时间?writeToFile是否使用缓冲流?另请参见。直接从bodyPart.getInputStream()读取数据或缓冲流,将1MB数据写入SD不到一秒钟,更改fetchsize属性没有任何效果。但是
props.setProperty(“mail.imaps.partialfetch”、“false”)
工作起来很有魅力!非常感谢,我在fetchingsize提示之后发现了它。是的,这会导致在对服务器的一次调用中读取整个身体部位数据。只要你有足够的内存来存储数据,那就很好了。如果你不在任何地方写入数据就读取数据,需要多长时间?如果在没有从消息读取数据的情况下将数据写入文件,需要多长时间?writeToFile是否使用缓冲流?另请参见。直接从bodyPart.getInputStream()读取数据或缓冲流,将1MB数据写入SD不到一秒钟,更改fetchsize属性没有任何效果。但是
props.setProperty(“mail.imaps.partialfetch”、“false”)
工作起来很有魅力!非常感谢,我在fetchingsize提示之后发现了它。是的,这会导致在一次调用服务时读取整个身体部位的数据