【问题标题】:JSONException: No value for id after completing paypal paymentJSONException:完成贝宝付款后,id 没有值
【发布时间】:2018-09-28 19:56:17
【问题描述】:

用户在我的 Android 应用上通过 PayPal 付款后,我希望显示 PaymentId,以及他们刚刚支付的状态和金额。当我尝试从 JSONObject 获取 id 时,当 logcat 中的 id 有值时,它一直说“id 没有值”。

ID 的值如图所示

当我到达textViewId.setText(jsonObject.getString("id")); 行时,它会跳转到 catch 并显示错误“No value for id”。即使在 logcat 中有一个值。

下面是我的代码。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == PAYPAL_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
                PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                if (confirmation != null)
                {
                    try
                    {
                        String paymentDetails = confirmation.toJSONObject().toString(4);
                        JSONObject jsonObject = new JSONObject(paymentDetails);
//                        String id = jsonObject.getString("id");
//                        String status = jsonObject.getString("state");
//                        startActivity(new Intent(getActivity(), PaymentDetails.class)
//                                .putExtra("PaymentDetails", paymentDetails)
//                                .putExtra("PaymentAmount", totalAmount + commisionAmount));

                        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                        View mView = getLayoutInflater().inflate(R.layout.popup_payment_successful, null);

                        alertDialog.setTitle("Payment Successful");
                        alertDialog.setView(mView);
                        final AlertDialog dialog = alertDialog.create();
                        dialog.show();

                        TextView textViewId = mView.findViewById(R.id.textId);
                        TextView textViewAmount = mView.findViewById(R.id.textAmount);
                        TextView textViewStatus = mView.findViewById(R.id.textStatus);
                        jsonObject.has("id");

                        textViewId.setText(jsonObject.getString("id"));
                        textViewAmount.setText((int) (totalAmount + commisionAmount));
                        textViewStatus.setText(jsonObject.getString("state"));
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED)
            {
                Toast.makeText(getActivity(), "Cancel", Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
        {
            Toast.makeText(getActivity(), "Invalid", Toast.LENGTH_SHORT).show();
        }
    }

【问题讨论】:

    标签: android json paypal try-catch


    【解决方案1】:

    显然你的idresponse jsonobject 里面,所以这样做

    JSONObject jsonObject = new JSONObject(paymentDetails);
    JSONObject jsonObject1 = jsonObject.optJSONObject("response");// return null or found object
    if(jsonObject1 != null){
        String id = jsonObject1.optString("id",""); //return value or empty string
        String status = jsonObject1.optString("state","");
    }
    

    【讨论】:

    • 谢谢你解决了我的问题
    • 我很高兴能帮上忙……快乐的编码
    猜你喜欢
    • 2014-03-13
    • 2011-07-25
    • 2017-07-12
    • 2016-06-17
    • 2012-08-30
    • 2013-03-31
    • 2012-07-20
    • 2016-05-27
    • 2018-06-17
    相关资源
    最近更新 更多