【发布时间】:2015-04-13 17:18:02
【问题描述】:
我需要将一个字符串变量从 onActivityResult 函数传递给另一个类,该类扩展了我在同一个 .java 文件中编写的 AsyncTask (sendData)。我需要传递名为js 的字符串变量。
这是我的代码
public class Donate extends Activity{
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
Log.i(TAG, confirm.toJSONObject().toString());
Log.i(TAG, confirm.getPayment().toJSONObject().toString());
Log.i(TAG, confirm.getProofOfPayment().toJSONObject().toString());
String js = confirm.getProofOfPayment().toJSONObject().toString();
new sendData().execute(js);
Toast.makeText(
getApplicationContext(),
"PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG)
.show();
Intent conf = new Intent(getApplicationContext(), PayConfirm.class);
startActivity(conf);
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(TAG,"An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
}
public class sendData extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/shareity/paypal.php/");
try {
HttpEntity httpEntity = new StringEntity(js);
httpPost.setEntity(httpEntity);
httpClient.execute(httpPost);
//String t = httpPost.toString();
Log.i(TAG, "Testiing");
Log.i(TAG, js);
}
catch (IOException e) {
}
return null;
}
}
【问题讨论】:
-
尝试这段代码会发生什么?
-
@EricS。在
sendData类中,js带有下划线并显示错误:js无法解析为变量
标签: android class android-asynctask