我在下面尝试{}将字符串确认设置为“已确认付款”。我想将setDescription()设置为String确认。如何将此字符串从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;
}
}
EDITED CODE,现在为什么每次按下按钮时,我的数据存储视图中都会有两个条目。
@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();
}
}
答案 0 :(得分:3)
你必须在EndpointsTask AsyncTask中创建一个构造函数,如下所示:
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
String confirmation;
public RouteAsyncRunnerUI(String confirmation ) {
this.confirmation = confirmation;
}
}
因此,您可以在EndpointsTask中调用字段确认作为确认,并使用它执行您喜欢的操作。
您可以在onActivityResult类中调用此构造函数,如下所示:
EndpointsTask task = new EndpointsTask(confirmation);
task.execute(this);
答案 1 :(得分:1)
我看不出你将Context的数组传入AsyncTask类的doInBackground方法的原因。要将字符串传入AsyncTask,您可以使用参数化类型
创建类public class EndpointsTask extends AsyncTask<String, Integer, Long>
您可以使用
执行任务new EndpointsTask().execute("any", "amount", "of", "strings);
这里有更多AsyncTasks
的文档