Android 获取Json数组凌空值
我创建了一个从服务器获取json的方法……我需要数据来设置条件,包括结果为->“[]”的空数组 所以,我的代码如下Android 获取Json数组凌空值,android,arrays,json,Android,Arrays,Json,我创建了一个从服务器获取json的方法……我需要数据来设置条件,包括结果为->“[]”的空数组 所以,我的代码如下 String url = "http://lineitopkal.com/android/api.php?case=status&id_user="+id_user; JsonArrayRequest schoolReq = new JsonArrayRequest(url, new Response.Listener<JSONArra
String url = "http://lineitopkal.com/android/api.php?case=status&id_user="+id_user;
JsonArrayRequest schoolReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
User user = new User();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
checkin_sekolah = obj.getString("status_checkin_sekolah");
checkin_wajah = obj.getString("status_checkin_wajah");
} catch (JSONException e) {
e.printStackTrace();
}
if(checkin_sekolah.equals("1") && checkin_wajah.equals("0")){
Intent intent = new Intent(MainActivity.this, FaceVerificationActivity.class);
intent.putExtra("id_sekolah",id_sekolah);
startActivity(intent);
} else if(checkin_sekolah.equals("1") && checkin_wajah.equals("1")){
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("id_sekolah",id_sekolah);
startActivity(intent);
}
else if(response.length()==0){
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putExtra("id_sekolah",id_sekolah);
intent.putExtra("nama",nama);
intent.putExtra("long", longi);
intent.putExtra("lat", lat);
startActivity(intent);
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(schoolReq);
}
我需要获取空数组“[]”来执行任务。有人知道怎么做吗?因为我所做的似乎不起作用您是否正在尝试处理空的JSON数组案例? 在代码中,for循环的条件是response.length,如果JSON数组为空,则该条件将为0。 您可以尝试像这样包装整个for循环
if(response.length()>0){
// Paste your for-loop here ( JSON array is not empty)
} else {
// Handle empty JSON array here
}
我不知道你是不是在要求这个。希望这有帮助