Java 改造和杰克逊并解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18666207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit and Hymanson and parsing JSON
提问by thd
I am using Retrofit with Hymanson. For some reasons I cannot parse the following JSON:
我正在与 Hymanson 一起使用 Retrofit。由于某些原因,我无法解析以下 JSON:
[
{
"ProfileImage": null,
"UserName": "joe"
},
{
"ProfileImage": "http://www.example.com/profiles/fileName1.jpg",
"UserName": "jane"
},
{
"ProfileImage": null,
"UserName": "john"
}
]
I get the this exception:
我得到这个例外:
Exception in thread "main" retrofit.RetrofitError: java.lang.ClassCastException:
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
cannot be cast to java.lang.Class
at retrofit.RetrofitError.unexpectedError(RetrofitError.java:41)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:294)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:182)
at com.sun.proxy.$Proxy5.getReplies(Unknown Source)
at Test.main(Test.java:25)
Caused by: java.lang.ClassCastException:
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
cannot be cast to java.lang.Class
at HymansonConverter.fromBody(HymansonConverter.java:27)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:274)
My code is as follow:
我的代码如下:
//File: Test.java:
//文件:Test.java:
public class Test {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new PropertyNamingStrategy
.PascalCaseStrategy());
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("http://www.example.com")
.setConverter(new HymansonConverter(mapper))
.build();
ListingService listingService = restAdapter.create(ListingService.class);
ArrayList<ListingReplyExt> replies = listingService.getReplies(123, 1, 10);
//More code...
}
}
}
//File: ListingService.java
//文件:ListingService.java
public interface ListingService {
@GET("/api/ListingRepliesService.svc/Get")
public ArrayList<ListingReplyExt> getReplies(
@Query("listingId") long listingId,
@Query("start") int start,
@Query("count") int count);
}
//File: ListingReplyExt.java:
//文件:ListingReplyExt.java:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListingReplyExt {
private String profileImage;
private String username;
@JsonProperty(value = "ProfileImage")
public String getProfileImage() {
return profileImage;
}
@JsonProperty(value = "ProfileImage")
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
@JsonProperty(value = "UserName")
public String getUsername() {
return username;
}
@JsonProperty(value = "UserName")
public void setUsername(String username) {
this.username = username;
}
}
Any help is appriciated.
任何帮助是appriciated。
UPDATE
更新
Sotirios Delimanolis' comment gave me some hints to resolve this issue.
Sotirios Delimanolis 的评论给了我一些解决这个问题的提示。
My HymansonConverter was originally this:
我的 HymansonConverter 最初是这样的:
public class HymansonConverter implements Converter {
private final ObjectMapper objectMapper;
public HymansonConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override public Object fromBody(TypedInput body, Type type) {
return objectMapper.readValue(body.in(), (Class<Object>) type);
}
//More code
}
I changed it to this:
我把它改成这样:
public class HymansonConverter implements Converter {
private final ObjectMapper objectMapper;
public HymansonConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override public Object fromBody(TypedInput body, Type type) {
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
return objectMapper.readValue(body.in(), javaType);
}
//More code
}
Now it works for me.
现在它对我有用。
回答by Thorre
Use Square's converter by adding this to your build.gradle:
通过将其添加到 build.gradle 来使用 Square 的转换器:
compile 'com.squareup.retrofit:converter-Hymanson:1.9.0'
Afterwards you can activate it by adding .setConverter(new HymansonConverter())
to your RestAdapter
builder.
之后,您可以通过添加.setConverter(new HymansonConverter())
到您的RestAdapter
构建器来激活它。
回答by RicardoDuarte
with new versions of OkHTTP it seems its done with:
使用新版本的 OkHTTP,它似乎完成了:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(HymansonConverterFactory.create())
.build();