In einer Android App muss ich überprüfen, ob eine JSON-Antwort null ist. Dies ist mein Code:Android überprüfen, ob JSON-Antwort null ist
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);
}
}
ich versuchte:
if (nombre.isEmpty()) {
if (nombre == null) {
if (nombre == "") {
Aber die App zeigt immer den anderen Teil der if-Bedingung. Und die Ausgabe, wenn es keine Daten aus JSON sind immer:
Agencia: null Direccion null Codigo: null
Wenn es Daten sind, ist der Ausgang der richtige Ausgang. Jede Hilfe ist willkommen
Sie haben Recht, in meinem Fall nombre == "null" ist richtig. Danke, – mvasco
Froh, dass es hilft! ;-) –