java - 改造正在返回一个空的响应体
问题描述
我正在尝试使用改造和 gson 来解析一些 JSON。但是,我得到一个空的响应体。当我尝试从对象打印信息时,我得到一个 NullPointerException。
我已经确保 URL 是正确的,并且我确信我的 POJO 也是正确的。我正在使用 jsonschema2pojo 来帮助创建 POJO 类。
这是我要解析的 JSON
{
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"root": {
"@id": "1",
"uri": {
"#cdata-section": "http://api.bart.gov/api/etd.aspx?cmd=etd&orig=ANTC&json=y"
},
"date": "05/07/2019",
"time": "02:00:01 PM PDT",
"station": [
{
"name": "Antioch",
"abbr": "ANTC",
"etd": [
{
"destination": "SFO/Millbrae",
"abbreviation": "MLBR",
"limited": "0",
"estimate": [
{
"minutes": "10",
"platform": "2",
"direction": "North",
"length": "1",
"color": "YELLOW",
"hexcolor": "#ffff33",
"bikeflag": "1",
"delay": "0"
},
{
"minutes": "25",
"platform": "2",
"direction": "North",
"length": "1",
"color": "YELLOW",
"hexcolor": "#ffff33",
"bikeflag": "1",
"delay": "0"
},
{
"minutes": "40",
"platform": "2",
"direction": "North",
"length": "1",
"color": "YELLOW",
"hexcolor": "#ffff33",
"bikeflag": "1",
"delay": "0"
}
]
}
]
}
],
"message": ""
}
}
这是我的改造客户/设置
public class RetrofitClient {
private static Retrofit sRetrofit = null;
public static Retrofit getClient(String baseUrl) {
if (sRetrofit == null) {
sRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return sRetrofit;
}
public interface ApiInterface {
// public API key
@GET("api/etd.aspx?cmd=etd&key=MW9S-E7SL-26DU-VV8V&orig=ANTC&json=y")
Call<Root> getData();
}
public class ApiUtils {
public static final String BASE_URL = "http://api.bart.gov/";
public static ApiInterface getApiInterface() {
return RetrofitClient.getClient(BASE_URL).create(ApiInterface.class);
}
最后,这是我的 onCreateView 中的代码
mService = ApiUtils.getApiInterface();
mService.getData().enqueue(new Callback<TrainArrival>() {
@Override
public void onResponse(Call<TrainArrival> call, Response<TrainArrival> response) {
if (response.isSuccessful()) {
trainData = response.body();
Log.d("TAG", new GsonBuilder().setPrettyPrinting().create().toJson(response));
} else {
int statusCode = response.code();
Log.d("TAG", Integer.toString(statusCode));
}
}
@Override
public void onFailure(Call<TrainArrival> call, Throwable t) {
Log.d("TAG", t.getMessage());
}
});
Log.d("TAG", trainData.getDate());
这是回应。正文标签为空。
{
"body": {},
"rawResponse": {
"body": {
"contentLength": 731,
"contentType": {
"charset": "utf-8",
"mediaType": "application/json; charset\u003dutf-8",
"subtype": "json",
"type": "application"
}
},
"code": 200,
"headers": {
"namesAndValues": [
"Cache-Control",
"no-cache",
"Pragma",
"no-cache",
"Content-Type",
"application/json; charset\u003dutf-8",
"Expires",
"-1",
"Server",
"Microsoft-IIS/8.5",
"Access-Control-Allow-Origin",
"*",
"X-AspNet-Version",
"4.0.30319",
"X-Powered-By",
"ASP.NET",
"Date",
"Wed, 08 May 2019 00:02:00 GMT",
"Content-Length",
"731"
]
},
"message": "OK",
"networkResponse": {
"code": 200,
"headers": {
"namesAndValues": [
"Cache-Control",
"no-cache",
"Pragma",
"no-cache",
"Content-Type",
"application/json; charset\u003dutf-8",
"Expires",
"-1",
"Server",
"Microsoft-IIS/8.5",
"Access-Control-Allow-Origin",
"*",
"X-AspNet-Version",
"4.0.30319",
"X-Powered-By",
"ASP.NET",
"Date",
"Wed, 08 May 2019 00:02:00 GMT",
"Content-Length",
"731"
]
},
"message": "OK",
"protocol": "HTTP_1_1",
"receivedResponseAtMillis": 1557273722035,
"request": {
"cacheControl": {
"immutable": false,
"isPrivate": false,
"isPublic": false,
"maxAgeSeconds": -1,
"maxStaleSeconds": -1,
"minFreshSeconds": -1,
"mustRevalidate": false,
"noCache": false,
"noStore": false,
"noTransform": false,
"onlyIfCached": false,
"sMaxAgeSeconds": -1
},
"headers": {
"namesAndValues": [
"Host",
"api.bart.gov",
"Connection",
"Keep-Alive",
"Accept-Encoding",
"gzip",
"User-Agent",
"okhttp/3.12.0"
]
},
"method": "GET",
"tags": {
"class retrofit2.Invocation": {
"arguments": [],
"method": {
"artMethod": 502253502104,
"override": false
}
}
},
"url": {
"host": "api.bart.gov",
"password": "",
"pathSegments": [
"api",
"etd.aspx"
],
"port": 80,
"queryNamesAndValues": [
"cmd",
"etd",
"key",
"MW9S-E7SL-26DU-VV8V",
"orig",
"ANTC",
"json",
"y"
],
"scheme": "http",
"url": "http://api.bart.gov/api/etd.aspx?cmd\u003detd\u0026key\u003dMW9S-E7SL-26DU-VV8V\u0026orig\u003dANTC\u0026json\u003dy",
"username": ""
}
},
"sentRequestAtMillis": 1557273721989
},
"protocol": "HTTP_1_1",
"receivedResponseAtMillis": 1557273722035,
"request": {
"headers": {
"namesAndValues": []
},
"method": "GET",
"tags": {
"class retrofit2.Invocation": {
"arguments": [],
"method": {
"artMethod": 502253502104,
"override": false
}
}
},
"url": {
"host": "api.bart.gov",
"password": "",
"pathSegments": [
"api",
"etd.aspx"
],
"port": 80,
"queryNamesAndValues": [
"cmd",
"etd",
"key",
"MW9S-E7SL-26DU-VV8V",
"orig",
"ANTC",
"json",
"y"
],
"scheme": "http",
"url": "http://api.bart.gov/api/etd.aspx?cmd\u003detd\u0026key\u003dMW9S-E7SL-26DU-VV8V\u0026orig\u003dANTC\u0026js
解决方案
这很可能是您的 POJO 类。我刚遇到这个问题。
尝试创建一个仅包含根及其 id 的类。目前。
class TrainArrival {
public TrainArrival() {
}
@SerializedName("root")
Root root;
class Root {
@SerializedName("@id")
public int id;
}
}
我刚刚注意到,您的getData()
返回值与您的response
. 我很惊讶它甚至编译。
它应该是
public interface ApiInterface {
// public API key
@GET("api/etd.aspx?cmd=etd&key=MW9S-E7SL-26DU-VV8V&orig=ANTC&json=y")
Call<TrainArrival> getData();
}
如果这不起作用,那么您需要通过创建自己的 OkHttpClient 来拦截原始响应以查看问题是否存在。
推荐阅读
- r - 创建分类变量(年龄类别)并应用于表格
- javascript - 调整 jQuery 数据表的列
- ios - 高度约束动画“跳跃”
- python - python AWS boto3为文件上传创建预签名url
- slack - 确定点击了消息中的哪个按钮
- node.js - 如何为加入的mysql表设置限制?
- python-3.x - 使用代数中的特殊级数解决这个级数(数学的使用)
- node.js - travis 上的两个独立部署配置
- python - 即使在 conda 中安装后也无法导入 Poppler
- mysql - Laravel json join,尝试加入具有多个 id 的 JSON 列和具有位置 id 的位置表,查询生成器