我正在尝试使用Retrofit 2.0和Jackson解析器,但它不起作用。它不会将值映射到模型。它给了我这个例外:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“total_count”(类models.GitResult),未标记为可忽略(3个已知属性:“incompleteResults”,“items”,“totalcount”])<登记/> at [Source:buffer((buffer(okio.GzipSource@2235992)))。inputStream(); line:1,column:21](通过参考链:models.GitResult [“total_count”])
但是我的模型和json响应中的键值是相同的。 这是模型:
private boolean incompleteResults;
private List<Item> items = new ArrayList<Item>();
private int totalcount;
json响应是这个
{
"incompleteResults": false,
"items": [{
"id": 748,
"login": "tom",
"score": 65.33592,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tom"
}, {
"id": 1,
"login": "mojombo",
"score": 41.894745,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/mojombo"
}, {
"id": 32314,
"login": "tmcw",
"score": 33.515797,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tmcw"
}, {
"id": 132166,
"login": "tommcfarlin",
"score": 33.515797,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tommcfarlin"
}, {
"id": 21292,
"login": "twilson63",
"score": 32.48973,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/twilson63"
}, {
"id": 85085,
"login": "tomwhite",
"score": 32.48973,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomwhite"
}, {
"id": 132554,
"login": "tmeasday",
"score": 32.48973,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tmeasday"
}, {
"id": 175,
"login": "tommorris",
"score": 29.326323,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tommorris"
}, {
"id": 83974,
"login": "Knio",
"score": 29.326323,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/Knio"
}, {
"id": 39635,
"login": "RandomEtc",
"score": 28.428514,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/RandomEtc"
}, {
"id": 565,
"login": "tomtaylor",
"score": 28.428514,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomtaylor"
}, {
"id": 391674,
"login": "tvlooy",
"score": 27.945265,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tvlooy"
}, {
"id": 667901,
"login": "tomjakubowski",
"score": 25.136848,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomjakubowski"
}, {
"id": 945979,
"login": "dribnet",
"score": 25.136848,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/dribnet"
}, {
"id": 70727,
"login": "waako",
"score": 25.136848,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/waako"
}, {
"id": 59207,
"login": "tomster",
"score": 25.136848,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomster"
}, {
"id": 238331,
"login": "brow",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/brow"
}, {
"id": 155977,
"login": "ToJans",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/ToJans"
}, {
"id": 379269,
"login": "tomasperezv",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomasperezv"
}, {
"id": 438654,
"login": "tejr",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tejr"
}, {
"id": 3010,
"login": "subblue",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/subblue"
}, {
"id": 11125,
"login": "almost",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/almost"
}, {
"id": 1678,
"login": "alltom",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/alltom"
}, {
"id": 26691,
"login": "tmilewski",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tmilewski"
}, {
"id": 60826,
"login": "rentalcustard",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/rentalcustard"
}, {
"id": 163497,
"login": "tomrittervg",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomrittervg"
}, {
"id": 75655,
"login": "tomlokhorst",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomlokhorst"
}, {
"id": 1287,
"login": "tomafro",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tomafro"
}, {
"id": 8482,
"login": "tominsam",
"score": 24.367298,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tominsam"
}, {
"id": 881466,
"login": "tommcc",
"score": 24.010895,
"siteAdmin": false,
"type": "User",
"url": "https://api.github.com/users/tommcc"
}],
"totalcount": 0
}
项目模型也正确完成。它适用于GsonConverterFactory
,但不适用于JacksonFactory
。
这是我的客户端设置:
private static GitApiInterface gitApiInterface ;
private static String baseUrl = "https://api.github.com" ;
public static GitApiInterface getClient() {
if (gitApiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(rest.JacksonConverterFactory.create())
.build();
gitApiInterface = client.create(GitApiInterface.class);
}
return gitApiInterface ;
}
和我的gradle依赖
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta4'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta1'
compile 'com.squareup.okhttp3:okhttp:3.0.0'
}
答案 0 :(得分:0)
在json你有
TOTAL_COUNT
在java中你有
TOTALCOUNT
您应该使用相同的名称或使用@JsonProperty注释,如下所示:
@JsonProperty("total_count)
private int totalcount;