我需要有关片段内Android集成的帮助,当代码在Activity中实现时,它可以很好地工作,但就我而言,我需要在片段中实现它。
我正在尝试从Fragment调用dropin ui,但无法这样做。
我遵循的步骤:-
1)我使用get_braintree_token(rootview.getContext())获取了Braintree令牌。
2)获得令牌后,我将其传递给onBraintreeSubmit(braintreeToken,viewb)
3)当它到达onBraintreeSubmit函数中的startActivityForResult()时,BrainTree UI不会膨胀,它直接移动到具有结果代码1和一些随机请求代码的onActivityResult()。
我已经在下面编写了我的函数。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_map, container, false);
get_braintree_token(rootview.getContext());
return rootview;
}
public void get_braintree_token(final Context viewb)
{
StringRequest stringRequest = new StringRequest(Request.Method.GET, "My server URL", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
braintreeToken = response.toString();
Log.e("Braintreetoken","token is:"+braintreeToken);
onBraintreeSubmit(braintreeToken, viewb);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
braintreeToken = null;
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
}
public void onBraintreeSubmit(String token, Context c) {
Log.e("tokenreceived",token);
DropInRequest dropInRequest = new DropInRequest();
dropInRequest.clientToken(token);
Intent drop = dropInRequest.getIntent(c);
startActivityForResult(drop, BRAINTREE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == 1) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
// use the result to update your UI and send the payment method nonce to your server
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}
最佳答案
传递令牌值,此处传递的是Json响应,它正在创建问题,因此从响应中提取令牌并将其传递给braintree方法,而不是传递json响应。