我在下面尝试将字符串确认设置为“已确认付款”。{} 我想 setDescription() 到字符串确认。如何将此字符串从 void onActivityResult 传递给公共类 EndpointsTask?
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
String confirmation = "payment confirmed";
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
}
else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
}
else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) {
Log.i("paymentExample", "An invalid payment was submitted. Please see the docs.");
}
}
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
ContactInfo note = new ContactInfo().setDescription(confirmation);
String noteID = new Date().toString();
note.setId(noteID);
EditText streetName;
streetName = (EditText) findViewById (R.id.streetAddress);
String streetInfo = streetName.getText().toString();
note.setStreetAddress(streetInfo);
EditText firstNameText;
firstNameText = (EditText) findViewById (R.id.firstName);
String firstNameInfo = firstNameText.getText().toString();
note.setNameFirst(firstNameInfo);
EditText lastNameText;
lastNameText = (EditText) findViewById (R.id.lastName);
String lastNameInfo = lastNameText.getText().toString();
note.setNameLast(lastNameInfo);
EditText emailText;
emailText = (EditText) findViewById (R.id.textemail);
String emailInfo = emailText.getText().toString();
note.setEmailAddress(emailInfo);
EditText zipText;
zipText = (EditText) findViewById (R.id.zipCode);
String zipInfo = zipText.getText().toString();
note.setZipCode(zipInfo);
EditText stateText;
stateText = (EditText) findViewById (R.id.state);
String stateInfo = stateText.getText().toString();
note.setState(stateInfo);
EditText phoneText;
phoneText = (EditText) findViewById (R.id.phoneNumber);
String phoneInfo = phoneText.getText().toString();
note.setPhone(phoneInfo);
ContactInfo result = endpoint.insertContactInfo(note).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
编辑后的代码,现在为什么每次点击按钮时我的数据存储视图中都会出现两个条目。
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
String confirmation = "payment confirmed";
EndpointsTask task = new EndpointsTask(confirmation);
task.execute(this);
// TODO: send 'confirm' to your server for verification.
// see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
// for more details.
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
}
else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
}
else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) {
Log.i("paymentExample", "An invalid payment was submitted. Please see the docs.");
}
}
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
String confirmation;
public EndpointsTask(String confirmation ) {
this.confirmation = confirmation;
}
protected Long doInBackground(Context... contexts) {
Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
ContactInfo note = new ContactInfo().setDescription(confirmation);
String noteID = new Date().toString();
note.setId(noteID);
EditText streetName;
streetName = (EditText) findViewById (R.id.streetAddress);
String streetInfo = streetName.getText().toString();
note.setStreetAddress(streetInfo);
EditText firstNameText;
firstNameText = (EditText) findViewById (R.id.firstName);
String firstNameInfo = firstNameText.getText().toString();
note.setNameFirst(firstNameInfo);
EditText lastNameText;
lastNameText = (EditText) findViewById (R.id.lastName);
String lastNameInfo = lastNameText.getText().toString();
note.setNameLast(lastNameInfo);
EditText emailText;
emailText = (EditText) findViewById (R.id.textemail);
String emailInfo = emailText.getText().toString();
note.setEmailAddress(emailInfo);
EditText zipText;
zipText = (EditText) findViewById (R.id.zipCode);
String zipInfo = zipText.getText().toString();
note.setZipCode(zipInfo);
EditText stateText;
stateText = (EditText) findViewById (R.id.state);
String stateInfo = stateText.getText().toString();
note.setState(stateInfo);
EditText phoneText;
phoneText = (EditText) findViewById (R.id.phoneNumber);
String phoneInfo = phoneText.getText().toString();
note.setPhone(phoneInfo);
ContactInfo result = endpoint.insertContactInfo(note).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
@Override
public void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
}