android - 我如何处理这两种情况的改造中的数据参数。如果成功为假且数据为空,则应用程序崩溃
问题描述
我的 API 响应:
{
success: true,
msg: 'Custom Success Message',
data : [Objects]
}
如果失败,响应是:
{
success: false,
msg: 'Custom Error Message',
data: null
}
我的问题是如何处理这两种场景的改造数据参数?
如果成功为false
且数据为 ,则应用程序崩溃null
。
解决方案
请检查下面的代码以检查获得响应时的空条件。
call1.enqueue(new Callback<YourModelClass>()
{
@Override
public void onResponse (Call < YourModelClass > call, Response < YourModelClass > response){
// this line must be write
if (Response.body != null) {
// try this code
JSONObject reader = new JSONObject("your response");
JSONArray data = reader.getString("data");
if(data!=null)
{
}
}
}
}
推荐阅读
- html - 在邮政编码文本框中的 5 个字符后显示连字符 - 未按预期工作
- pandas - 从平面 DataFrame 计算热图的相交和
- java - PDF 使用 Java PDFBox 2.0.21 卡在“打印”状态
- java - Spring Boot 2 事务注释不起作用
- django - Django - 运行 collectstatic 收集静态文件,但收集项目目录中的所有文件
- open-policy-agent - 无法读取 kubernetes 属性
- json - 在colab中使用python创建一个json文件并保存到驱动
- laravel - Laravel 分页(带外键)
- python - Python Kolmogorov-Smirnov (KS) 测试结果不一致
- ssl - 多个站点和多个 SSL 证书的 IIS10 SSL 配置