我有一个 jsonArray ,当数组为空时,我遇到了一个崩溃,说 application layer
。但是如果数组为null,我想显示一个文本。我怎样才能做到这一点?最近两天陷入了这个问题。 JSON 数组在这里:
app stopped unfortunately
这是我的java代码:
[{"head_name":null,"amount":null,"vat":null,"for_month":null,"status":null}]
答案 0 :(得分:2)
使用
paymentInfo_jul.isNull("head_name")
https://developer.android.com/reference/org/json/JSONObject.html#isNull%28java.lang.String%29
Config.paymentInfo_jul.add( paymentInfo_jul.isNull("amount")?"N/A":paymentInfo_jul.getString("amount"));// Amount_jul in index (0)
Config.paymentInfo_jul.add(paymentInfo_jul.isNull("vat")?"N/A":paymentInfo_jul.getString("vat"));// VAT_jul in index (1)
Config.paymentInfo_jul.add(paymentInfo_jul.isNull("status")?"Not Paid":paymentInfo_jul.getString("status"));// VAT_jul in index (2)
或者可以使用optString
函数
获取与密钥关联的可选字符串。它返回 如果没有这样的密钥,则为defaultValue。
public String optString(String key, String defaultValue) {
Config.paymentInfo_jul.add( paymentInfo_jul.optString("amount","N/A"));
// will return "N/A" if value is not or not found
答案 1 :(得分:1)
试试这个代码段:
if(response_jul.toString().equals("Null")){
Log.i("jsa","null");
}
答案 2 :(得分:0)
试试这个:
Config.paymentInfo_jul.add(paymentInfo_jul.getString("amount") == null ? YOUR_DEFAULT_VALUE : paymentInfo_jul.getString("amount"));
答案 3 :(得分:0)
对于这些问题,您必须使用try的梯形图并在代码中捕获代码,
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();
}
在你正在使用的每个地方,如果使用try catch而不是if和else。
答案 4 :(得分:0)
通过这样做解决了我的问题:
if (status_jul.equals("null")){
textView_status_jul.setText(" Not Paid");
}else {
fixedAmount_jul();
}
答案 5 :(得分:0)
首先在xml布局上创建一个textview,当jsonarray为null时显示文本,可见性为GONE
<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();
}
}