Reputation: 85
I create a method to get json from server...And I need the data to make conditional including the empty array with result -> '[ ]'
So, as my code below
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);
}
I need to get the empty array '[ ]' to perform the task. Anyone know how to do that? Cuz what I did doesn't seem work
Upvotes: 0
Views: 797
Reputation: 445
Are you trying to handle the empty JSON array case?
In your code, your condition for the "for-loop" is response.length()
which will be 0 if the JSON array is empty.
You can try to wrap the entire for loop like this
if(response.length()>0){
// Paste your for-loop here ( JSON array is not empty)
} else {
// Handle empty JSON array here
}
I am not sure if you are asking for this or not. Hope this help
Upvotes: 1