我有以下JSON从NYTimes TopStories API返回;Gson没有正确序列化JSON到POJO与改造
{
"status": "OK",
"copyright": "Copyright (c) 2015 The New York Times Company. All Rights Reserved.",
"last_updated": "2015-07-03T01:25:02-05:00",
"num_results": 21,
"results": [
{
"section": "World",
"subsection": "Africa",
"title": "Tunisia Jihadist Died in Libya Strike, U.S. Official Says",
"abstract": "Seifallah Ben Hassine, one of Osama bin Laden’s top lieutenants and Tunisia’s most wanted jihadist, was killed in an American airstrike in Libya last month, a senior United States official said.",
"url": "http://www.nytimes.com/2015/07/03/world/africa/jihadist-from-tunisia-died-in-strike-in-libya-us-official-says.html",
"byline": "By CARLOTTA GALL and ERIC SCHMITT",
"item_type": "Article",
"updated_date": "2015-07-02T21:18:07-5:00",
"created_date": "2015-07-02T21:18:10-5:00",
"published_date": "2015-07-03T04:00:00-5:00",
"material_type_facet": "News",
"kicker": "",
"des_facet": [
"Terrorism",
"Defense and Military Forces"
],
"org_facet": "",
"per_facet": [
"Hassine, Seifallah Ben",
"bin Laden, Osama"
],
"geo_facet": [
"Tunisia",
"Libya"
],
"multimedia": ""
}
]
}
纽约时报返回此JSON
与text/json
头,我不知道是否有这个任何影响。
我正在使用Retrofit,并且我的onSuccess方法在回调中从未达到。我宣布,像这样的模型类:
主模型类
public class TopStories {
@SerializedName("status")
private String mStatus;
public String getStatus() {
return mStatus;
}
@SerializedName("copyright")
private String mCopyright;
public String getCopyright() {
return mCopyright;
}
@SerializedName("last_updated")
private String mLastUpdated;
public String getLastUpdated() {
return mLastUpdated;
}
@SerializedName("num_results")
private int mNumResults;
public int getNumResults() {
return mNumResults;
}
@SerializedName("results")
private List<Result> mResults;
public List<Result> getResults() {
return mResults;
}
}
结果模型类
public class Result {
@SerializedName("section")
private String mSection;
public String getSection() {
return mSection;
}
@SerializedName("subsection")
private String mSubSection;
public String getSubSection() {
return mSubSection;
}
@SerializedName("title")
private String mTitle;
public String getTitle() {
return mTitle;
}
@SerializedName("abstract")
private String mAbstract;
public String getAbstract() {
return mAbstract;
}
@SerializedName("url")
private String mUrl;
public String getUrl() {
return mUrl;
}
@SerializedName("byline")
private String myByLine;
public String getMyByLine() {
return myByLine;
}
@SerializedName("item_type")
private String mItemType;
public String getItemType() {
return mItemType;
}
@SerializedName("updated_date")
private String mUpdatedDate;
public String getUpdatedDate() {
return mUpdatedDate;
}
@SerializedName("created_date")
private String mCreatedDate;
public String getCreatedDate() {
return mCreatedDate;
}
@SerializedName("multimedia")
private List<Multimedia> mMultimedia;
public List<Multimedia> getMultimedia() {
return mMultimedia;
}
}
你的想法,我有"multimedia"
另一个模型类,确定继续我创建了一个RestAdapter
像这样:
private NYTimesService() {
mAsyncRestAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addEncodedQueryParam("api-key", API_KEY);
}
})
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
}
而且我有一个API接口,像这样:
public interface ITopStories {
@Headers("Content-Type: text/json")
@GET("/topstories/v1/home.json")
void getTopStories(Callback<TopStories> callback);
}
而且我Callback<T>
的定义,像这样:
@Subscribe
public void onLoadTopStories(LoadTopStories loadTopStories) {
Log.d(TAG, "onLoadTopStories");
Callback<TopStories> callback = new Callback<TopStories>() {
@Override
public void success(TopStories topStories, Response response) {
Log.d(TAG, "onSuccess");
mBus.post(new LoadedTopStories(topStories));
}
@Override
public void failure(RetrofitError error) {
}
};
sClient.getTopStories(callback);
}
的Log.d(TAG, "onLoadTopStories");
被称为细,问题是,Log.d(TAG, "onSuccess");
永远达不到。这里有什么问题?
一些注意事项:
- “结果”可以是一个数组或一个字符串,如果其空
- 以“多媒体”
- 同样的事情是什么令我感到困惑的是,
.getCopyright()
或getStatus()
不被序列化到首先在TopStories.java
而且Retrofit
具有与GET
请求,并将其初始没有问题TES得当:
更新
添加日志报表onFailure
,我得到以下几点:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException :预计BEGIN_ARRAY,但在第1行10819路径$ .results [4]的STRING。多媒体
多媒体可以是一个数组或一个空字符串
"multimedia": [
{
"url": "http://static01.nyt.com/images/2015/07/04/world/04Greece1-web/04Greece1-web-thumbStandard.jpg",
"format": "Standard Thumbnail",
"height": 75,
"width": 75,
"type": "image",
"subtype": "photo",
"caption": "A Greek Orthodox priest giving money to a man on a street in Thessaloniki, Greece, on Friday.",
"copyright": "Giannis Papanikos/Associated Press"
},
{
"url": "http://static01.nyt.com/images/2015/07/04/world/04Greece1-web/04Greece1-web-thumbLarge.jpg",
"format": "thumbLarge",
"height": 150,
"width": 150,
"type": "image",
"subtype": "photo",
"caption": "A Greek Orthodox priest giving money to a man on a street in Thessaloniki, Greece, on Friday.",
"copyright": "Giannis Papanikos/Associated Press"
},
{
"url": "http://static01.nyt.com/images/2015/07/04/world/04Greece1-web/04Greece1-web-articleInline.jpg",
"format": "Normal",
"height": 127,
"width": 190,
"type": "image",
"subtype": "photo",
"caption": "A Greek Orthodox priest giving money to a man on a street in Thessaloniki, Greece, on Friday.",
"copyright": "Giannis Papanikos/Associated Press"
},
{
"url": "http://static01.nyt.com/images/2015/07/04/world/04Greece1-web/04Greece1-web-mediumThreeByTwo210.jpg",
"format": "mediumThreeByTwo210",
"height": 140,
"width": 210,
"type": "image",
"subtype": "photo",
"caption": "A Greek Orthodox priest giving money to a man on a street in Thessaloniki, Greece, on Friday.",
"copyright": "Giannis Papanikos/Associated Press"
}
]
也许失败叫什么名字? –
哦,是的,我忘了在onFailure中包含Log语句,更新了问题。 – AndyRoid
不知道为什么有人反对,这是一个合法的问题。 – AndyRoid