标题为How do you eliminate progress dialog on response of retrofit if it gets "[]"
我的response.body()。getAsJsonArray()获取[]。我希望它得到[]我的progressdialog会解雇。
我已经尝试了
response.body()。getAsJsonArray()。toString()==" []&#34 ;; response.body()。getAsJsonArray()。toString()== null; 。response.body()getAsJsonArray()等于(" []&#34);
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
if (response.body().getAsJsonArray() == null) {
Toast.makeText(getContext(), "No Data to display.", Toast.LENGTH_LONG).show();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
} else if (response.body().getAsJsonArray().equals("[]")) {
Toast.makeText(getContext(), "No Data to display.", Toast.LENGTH_LONG).show();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
} else {
// Showing progress dialog
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Plotting.... Please wait.");
pDialog.setCancelable(false);
pDialog.show();
System.out.println(response.body().getAsJsonArray() + " JsonArray");
final PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < response.body().getAsJsonArray().size(); i++) {
JsonElement location_array = response.body().getAsJsonArray().get(i);
JsonObject location_obj = location_array.getAsJsonObject();
String loc = location_obj.get("location").toString();
String locString = loc;
locString = locString.replace("\"", "");
String location = String.valueOf(locString);
JsonElement lat_array = response.body().getAsJsonArray().get(i);
JsonObject lat_obj = lat_array.getAsJsonObject();
String lati = lat_obj.get("lat").toString();
String latiString = lati;
latiString = latiString.replace("\"", "");
String lat = String.valueOf(latiString);
JsonElement lng_array = response.body().getAsJsonArray().get(i);
JsonObject lng_obj = lng_array.getAsJsonObject();
String longi = lng_obj.get("lng").toString();
String longiString = longi;
longiString = longiString.replace("\"", "");
String lng = String.valueOf(longiString);
polylineOptions.color(Color.RED);
polylineOptions.width(3);
if (lat == null || lat.equals("null") || (lng == null && lng.equals("null") || (lat == "" || (lng == "") ||
(lat.isEmpty()) || (lng.isEmpty())))) {
Toast.makeText(getContext(), "Snail trail has no value.", Toast.LENGTH_LONG).show();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
mMapSnailTrail.addPolyline(polylineOptions);
}
}
答案 0 :(得分:1)
您应该检查 SIZE
else if (response.body().getAsJsonArray().size()==0)
{
Toast.makeText(getContext(), "No Data to display.", Toast.LENGTH_LONG).show();
if (pDialog.isShowing()) {
pDialog.dismiss();
}