在Android应用程序中,我需要检查JsON响应是否为null.
这是我的代码:
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); }}
我尝试过:
if (nombre.isEmpty()) {if (nombre == null) {if (nombre == "") {
但是应用程序始终显示if条件的else部分.
如果没有来自JsON的数据,则输出始终为:
Agencia:空
无效null
Codigo:空
如果有数据,则输出为正确的输出.
欢迎任何帮助
解决方法:
在androID中,请使用TextUtils.isEmpty()来检查是否为null.
但我认为在您的情况下,nombre不为空.
字符串nombre =“ null”;可能在您的情况下.
以上是内存溢出为你收集整理的Android检查JSON响应是否为空全部内容,希望文章能够帮你解决Android检查JSON响应是否为空所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)