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

Java 当JSON值为空时,应用程序崩溃,java,android,json,Java,Android,Json,我有一个jsonArray,当数组为空时,我收到一个崩溃消息,说应用程序不幸停止了。但是如果数组为空,我想显示一个文本。我该怎么做?在过去的两天里,我陷入了这个问题JSON数组如下: [{"head_name":null,"amount":null,"vat":null,"for_month":null,"status":null}] 以下是我的java代码: String url_payment_jul = Config.PAYMENT_JUL_URL+"?reference="+Confi

我有一个jsonArray,当数组为空时,我收到一个崩溃消息,说
应用程序不幸停止了
。但是如果数组为空,我想显示一个文本。我该怎么做?在过去的两天里,我陷入了这个问题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();
        }
    });
String url\u payment\u jul=Config.payment\u jul\u url+”?reference=“+Config.userInfo.get(5);
JsonArrayRequest req_jul=新JsonArrayRequest(url_payment_jul,
新的Response.Listener(){
@凌驾
公共无效响应(JSONArray响应){
Log.d(“我的数据”,response_jul.toString());
试一试{
//解析json数组响应
//循环遍历每个json对象
字符串jsonResponse=“”;
对于(int i=0;i
使用

或者,可以使用
optString
函数进一步简化

获取与键关联的可选字符串。它返回 如果没有这样的键,则默认值

 public String optString(String key, String defaultValue) {
试试这个:

Config.paymentInfo_jul.add(paymentInfo_jul.getString("amount") == null ? YOUR_DEFAULT_VALUE : paymentInfo_jul.getString("amount"));
请尝试以下代码段:

if(response_jul.toString().equals("Null")){
   Log.i("jsa","null");
}

对于这些问题,您必须使用try-and-catch-ladder代码让您的代码进入

try{
    String status_jul = Config.paymentInfo_jul.get(2);
fixedAmount_jul();                            
textView_status_jul.setText(" Not Paid");

   }catch {
   Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
           }
}
在你的for循环中

try
{
 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)
}
catch(Exception e)
{
       Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();

}

在每个使用if和else的地方,请使用try catch而不是if和else。

这样做解决了我的问题:

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

                    }else {
                        fixedAmount_jul();
                    }

首先在xml布局上创建一个文本视图,当jsonarray为空时显示文本,可见性为空

<TextView
        android:id="@+id/empty_view"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="You have no new  notifications"
        android:visibility="gone" />

在onResponse()方法上,在应用for循环之前,检查数组的长度,如果长度为0,则使textview的可见性与

if(response_jul.length()==0){
emptyView.setVisibility(View.VISIBLE);
}else{
 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();
                        }

}
if(响应长度()==0){
emptyView.setVisibility(View.VISIBLE);
}否则{
对于(int i=0;i
当数组为空时显示Json
 if (status_jul.equals("null")){
                        textView_status_jul.setText(" Not Paid");

                    }else {
                        fixedAmount_jul();
                    }
<TextView
        android:id="@+id/empty_view"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="You have no new  notifications"
        android:visibility="gone" />
if(response_jul.length()==0){
emptyView.setVisibility(View.VISIBLE);
}else{
 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();
                        }

}