Android checks whether the JSON response is empty
•
Android
In the Android application, I need to check whether the JSON response is null. This is my code:
private void showJSON(String response) {
String nombre = "";
String direccion = "";
String codigo = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
nombre = collegeData.getString(Config.KEY_NAME);
direccion = collegeData.getString(Config.KEY_ADDRESS);
codigo = collegeData.getString(Config.KEY_VC);
} catch (JSONException e) {
e.printStackTrace();
}
if (nombre.isEmpty()) {
nombre = "AGENCIA NO ENCONTradA";
textViewResult.setText("Agencia:\t" + nombre);
}
else {
textViewResult.setText("Agencia:\t" + nombre + "\nDireccion:\t" + direccion + "\nCodigo:\t" + codigo);
}
}
I tried:
if (nombre.isEmpty()) {
if (nombre == null) {
if (nombre == "") {
However, the application always displays the else part of the if condition. If there is no data from JSON, the output is always:
Agency: null invalid null codego: null
If there is data, the output is the correct output. Any help is welcome
resolvent:
In Android, please use textutils. Isempty() to check whether it is null. But I think in your case, nombre is not empty. String nombre = "null"; Maybe in your case
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码