【发布时间】:2021-06-02 15:18:27
【问题描述】:
我有一个 Jackson 服务器,可以将 Service 对象解析为 JSON,然后将其发送到 Android。在 Android 中,我有相同的 Service 类,但 gson 总是返回一个空对象。
服务类:
public class Service {
public String ServiceName;
public ArrayList<String> ParamList;
public ArrayList<String> ParamType;
public Service(String sn,ArrayList<String> pl,ArrayList<String> pt) {
this.ServiceName = sn;
this.ParamList = pl;
this.ParamType = pt;
// TODO Auto-generated constructor stub
}
JSON 字符串:
{"ParamList":[],"ParamType":[],"ServiceName":"ServiceX"}
安卓代码:
gson.fromJson(response,Client.Service.class)
Json 字符串来自日志,所以我知道服务器工作正常。在 Gradle 的模块路径中添加依赖:
implementation 'com.google.code.gson:gson:2.8.6'
【问题讨论】:
-
在将
response传递给fromJson之前,您是否确定您的response不为空或null?