Reputation: 5101
I am using Volley to get JSON from remote PHP script. Sometimes, the JSON array is empty.
This my code:
StringRequest stringRequest=new StringRequest(Request.Method.GET, HI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray array=jsonObject.getJSONArray("data");
for (int i=0; i<array.length(); i++ ){
JSONObject ob=array.getJSONObject(i);
Notificacion listData=new Notificacion(ob.getString("id_notificacion")
,ob.getString("texto"),
ob.getString("fecha"),
ob.getString("nombre"),
ob.getString("nombre")+" "+ob.getString("apellidos"),
ob.getString("profesor"),
ob.getString("tutor"),
ob.getString("estado"),
ob.getString("envia_tutor"),
ob.getString("envia_profesor")
);
list_data.add(listData);
spinner.setVisibility(View.GONE);
}
rv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("arry","array lengt error:"+error);
Toast.makeText(getActivity(),
"No tienes notificaciones.", Toast.LENGTH_LONG)
.show();
spinner.setVisibility(View.GONE);
}
});
RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
If the JSON array is empty, then I am getting the response listener message saying that data[] is empty, but the Toast is not showing and also the spinner visibility is not gone as expected.
How should I change in my code to launch a Toast and to set the spinner visibility to GONE when the received JSON array is empty?
Upvotes: 1
Views: 184
Reputation: 6732
You should some changes in your code like this:
StringRequest stringRequest=new StringRequest(Request.Method.GET, HI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray array=jsonObject.getJSONArray("data");
if (array.length()==0){
Toast.makeText(getActivity(),
"No tienes notificaciones.", Toast.LENGTH_LONG)
.show();
spinner.setVisibility(View.GONE);
}else{
for (int i=0; i<array.length(); i++ ){
JSONObject ob=array.getJSONObject(i);
Notificacion listData=new Notificacion(ob.getString("id_notificacion")
,ob.getString("texto"),
ob.getString("fecha"),
ob.getString("nombre"),
ob.getString("nombre")+" "+ob.getString("apellidos"),
ob.getString("profesor"),
ob.getString("tutor"),
ob.getString("estado"),
ob.getString("envia_tutor"),
ob.getString("envia_profesor")
);
list_data.add(listData);
spinner.setVisibility(View.VISIBLE);
}
rv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
spinner.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("arry","array lengt error:"+error);
spinner.setVisibility(View.GONE);
}
});
RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
Upvotes: 1
Reputation: 3351
public void onResponse(String response)
means you request get response successfully and parse successfully, but this callback did not means the data value is not empty!
public void onErrorResponse(VolleyError error)
means the request failed to get response, maybe error http code, or request successfully but parse failed.
So as you wish you want to do something when the request is success but data array
is empty, you should add a logic both in the public void onResponse(String response)
method to check the successfully response
but data is empty situation
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("data");
if (array == null || array.length() == 0) {
//data array is empty
// add the logic you want
} else {
//data array not empty
for (int i = 0; i < array.length(); i++) {
JSONObject ob = array.getJSONObject(i);
Notificacion listData = new Notificacion(ob.getString("id_notificacion")
, ob.getString("texto"),
ob.getString("fecha"),
ob.getString("nombre"),
ob.getString("nombre") + " " + ob.getString("apellidos"),
ob.getString("profesor"),
ob.getString("tutor"),
ob.getString("estado"),
ob.getString("envia_tutor"),
ob.getString("envia_profesor")
);
list_data.add(listData);
spinner.setVisibility(View.GONE);
}
rv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Upvotes: 1