1

我有一个在谷歌驱动器中创建谷歌文档的代码。当用户提供不正确的凭据时,应用程序应该弹出另一个对话框供用户更正其凭据。一旦entry = docService.insert(feedUrl, entry);throws ,该对话框就不会再次显示AuthenticationException。首先loginDialog()由主要活动调用,其余由应用程序处理。这是我的代码:

public void loginDialog(String error) {

        dialog = new Dialog(_context);
        dialog.setContentView(R.layout.login_dialog);
        dialog.setTitle("Enter your gmail account");

        dialog.show();

        Button saveLogin = (Button) dialog.findViewById(R.id.saveLogin);
        Button cancelled = (Button) dialog.findViewById(R.id.cancelLogin);
        final EditText userName = (EditText) dialog
                .findViewById(R.id.userNameEditText);
        final EditText password = (EditText) dialog
                .findViewById(R.id.passwordEditText);
        TextView errorField = (TextView) dialog
                .findViewById(R.id.loginRegisterErrorTextView);
        errorField.setText(error);

        userName.setText(getUsername()); // if the username has been saved
                                            // getUserName()
                                            // will return the username,
                                            // otherwise it will return an empty
                                            // string.

        saveLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.setCancelable(true);
                USERNAME = userName.getText().toString();
                PASSWORD = password.getText().toString();
                saveUsername(USERNAME);
                upload();
                dialog.cancel();
            }
        });
        cancelled.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.setCancelable(true);
                dialog.cancel();

            }
        });
    }

    private void upload() {
        SpreadSheetAsyncTask sprdSheetAsyncTsk = new SpreadSheetAsyncTask();

        sprdSheetAsyncTsk.execute();

    }

    public class SpreadSheetAsyncTask extends AsyncTask<Void, Void, Void> {

        private ProgressDialog mProgressDialog;

        @Override
        protected Void doInBackground(Void... params) {
            spreadSheetService = new SpreadsheetService("Salary_Calculator");
            docService = new DocsService("Salary_Calculator");
            try {

                spreadSheetService.setUserCredentials(USERNAME, PASSWORD);
                docService.setUserCredentials(USERNAME, PASSWORD);

                URL feedUrl = new URL(
                        "https://docs.google.com/feeds/default/private/full/");
                // URL tmpFeedUrl = new URL(
                // "https://spreadsheets.google.com/feeds/worksheets/key/private/full");

                DocumentEntry entry = new DocumentEntry();

                Calendar timeRightNow = GregorianCalendar.getInstance();

                entry.setTitle(new PlainTextConstruct(
                        "Salary Calculator Spreadsheet "
                                + timeRightNow.get(Calendar.DAY_OF_MONTH) + "/"
                                + (timeRightNow.get(Calendar.MONTH) + 1) + "/"
                                + timeRightNow.get(Calendar.YEAR)));

                // Category object = new Category("spreadsheet",
                // "http://schemas.google.com/docs/2007#spreadsheet");
                //
                //
                // entry.getCategories().add(object);                       

                entry = docService.insert(feedUrl, entry);

            } catch (AuthenticationException e) {
                cancel(true);
                dialog.cancel();
                 mProgressDialog.dismiss();
                loginDialog("Wrong username or password");
            } catch (IOException e) {
                e.printStackTrace();
                dialog.cancel();
                 mProgressDialog.dismiss();
                loginDialog("Target server failed to respond with a valid HTTP response");
                return null;
            } catch (ServiceException e) {
                cancel(true);
                dialog.cancel();
                 mProgressDialog.dismiss();
                loginDialog("Service Error");
            } catch (Exception e) {
                dialog.cancel();
                 mProgressDialog.dismiss();
                loginDialog("Error");
            }

            return null;
        }
4

0 回答 0