JSONException:完成paypal付款后没有id的值

时间:2018-04-18 14:13:34

标签: 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"));行时,它会跳转到catch并显示错误“no id 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();
        }
    }

1 个答案:

答案 0 :(得分:2)

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