有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当JSON值为空时,java应用程序崩溃

我有一个jsonArray,当数组为空时,我收到一个崩溃消息说app stopped unfortunately。但是如果数组为空,我想显示一个文本。我该怎么做?在过去的两天里被这个问题困住了JSON数组如下:

[{"head_name":null,"amount":null,"vat":null,"for_month":null,"status":null}]

以下是我的java代码:

String url_payment_jul = Config.PAYMENT_JUL_URL+"?reference="+Config.userInfo.get(5);
    JsonArrayRequest req_jul = new JsonArrayRequest(url_payment_jul,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response_jul) {
                    Log.d("my data", response_jul.toString());

                    try {
                        // Parsing json array response
                        // loop through each json object
                        String jsonResponse = "";
                        for (int i = 0; i < response_jul.length(); i++) {

                            JSONObject paymentInfo_jul = (JSONObject)response_jul.get(i);

                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("amount"));// Amount_jul in index (0)
                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("vat"));// VAT_jul in index (1)
                            Config.paymentInfo_jul.add( paymentInfo_jul.getString("status"));// Status_jul in index (2)
                        }

                        String status_jul = Config.paymentInfo_jul.get(2);

                        if (status_jul == null){
                            textView_status_jul.setText(" Not Paid");

                        }else {
                            fixedAmount_jul();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(",y error", "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

共 (0) 个答案