본문 바로가기

Android

[Gmail API] Load Mail Detail Info

메일 목록에서 받는 정보는 간략한 정보들로 구성되어있지만

메일을 눌러서 내용을 볼 때엔 상세정보가 필요함

    public static void loadMailDetail(Context context, Account account, String mailId, GmailNetworkListener listener) {
        try {
            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(MAIL_SCOPE));
            credential.setSelectedAccount(account);

            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
            new GetMailDetailTask(account, service, mailId, listener).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class GetMailDetailTask extends AsyncTask<Void, Void, Message> {

        private Account account;
        private Gmail service;
        private String mailId;
        private GmailNetworkListener listener;

        public GetMailDetailTask(Account account, Gmail service, String mailId, GmailNetworkListener listener) {
            this.account = account;
            this.service = service;
            this.mailId = mailId;
            this.listener = listener;
        }

        @Override
        protected Message doInBackground(Void... voids) {
            if (listener == null) return null;

            try {
                return service.users().messages().get(account.name, mailId).execute();
            } catch (IOException e) {
                e.printStackTrace();
                listener.onFailure(e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Message result) {
            super.onPostExecute(result);
            if (listener != null) {
                listener.onSuccess(result);
            }
        }
    }