android - JSON异常 : No value for id after completing paypal payment

标签 android json paypal try-catch

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

ID 的值如图所示

Id not null

Logcat Where it keeps breaking

当我到达 textViewId.setText(jsonObject.getString("id")); 行时,它跳转到捕获并显示错误“没有 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();
        }
    }

最佳答案

显然你的 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","");
}

关于android - JSON异常 : No value for id after completing paypal payment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49901994/

相关文章:

android - 从 SD 卡获取图像 - android

java - 将文件读入数组后android NULL值

java - 如何在volley中传递header api key

PayPal 此处文档

php - 使用 php 的自动付款通知

android - AsyncTask 致命异常 #1

java - Android 动态 Mqtt 主题未从代理接收消息

Javascript - 如何获取 list json 并将其用作对象?

json - Blogger API 缺少内容数据

paypal - MassPay 对多个收款人的单一好/坏确认