Google ドライブに Google ドキュメントを作成するコードがあります。ユーザーが誤った資格情報を提供すると、アプリケーションはユーザーが資格情報を修正するための別のダイアログをポップアップする必要がありentry = docService.insert(feedUrl, entry);
ます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;
}