1

我在下面尝试将字符串确认设置为“已确认付款”。{} 我想 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();
}

}

4

2 回答 2

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);
于 2013-08-29T15:33:53.173 回答
1

我看不出您将 Context 数组传入 AsyncTask 类中的 doInBackground 方法的原因。要将字符串传递到 AsyncTask 中,您可以使用参数化类型创建类

public class EndpointsTask extends AsyncTask<String, Integer, Long>

你可以使用执行任务

new EndpointsTask().execute("any", "amount", "of", "strings);

这是AsyncTasks 的更多文档

于 2013-08-29T15:37:16.410 回答