我正在尝试实现Android spring get请求。当我运行应用程序时,我遇到了以下错误。
无法提取回复:找不到合适的HttpMessageConverter 响应类型[com.example.userprofiledemo.UserProfile]和内容 输入[text / html; charset = UTF-8]
Respose Body包含嵌套类和类列表,Respose xml如下所示:
<UserProfile xmlns="http://schemas.datacontract.org/2004/07/"xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Address>Degisim A Blok Asmakat</Address>
<City>Adana</City>
<UserBookingInfo>
<ArrivalDate>2012-01-03T00:00:00</ArrivalDate>
<CourseTypeCode>ILS</CourseTypeCode>
</UserBookingInfo>
<UserPhotoList>
<UserPhoto>
<LikeCount>10</LikeCount>
<UserPhotoId>123</UserPhotoId>
</UserPhoto>
<UserPhoto>
<LikeCount>11</LikeCount>
<UserPhotoId>129</UserPhotoId>
</UserPhoto>
<UserPhoto>
<LikeCount>11</LikeCount>
<UserPhotoId>129</UserPhotoId>
</UserPhoto>
</UserPhotoList>
</UserProfile>
注意: -
UserProfile(Userprofile.java)
是Root Class或Node UserBookingInfo(UserBookingInfo.java)
是UserProfile
中的元素。UserPhoteList(UserPhoteList.java)
是UserPhotos(UserPhote.java)
UsersProfile
的列表
醇>
我正在使用Spring Android解析响应。
我的REST客户端PopulatePojo.java
public class PopulatePojo {
public UserProfile GetUserProfile() {
RestTemplate restTemplate = new RestTemplate(true);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
FormHttpMessageConverter msgConverter = new FormHttpMessageConverter();
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
UserProfile userProfile = null;
try {
userProfile = restTemplate.getForObject(url, UserProfile.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return userProfile;
}
}
Bean类UserProfile.java
@Root
public class UserProfile {
@Element(name = "Address", required = false)
private String address;
@Element(name="City",required=false)
private String city;
@Element(name="UserBookingInfo",required=false)
private UserBookingInfo userBookingInfo;
@Element(name = "UserPhotoList")
private UserPhotoList userPhotoList;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public UserBookingInfo getUserBookingInfo() {
return userBookingInfo;
}
public void setUserBookingInfo(UserBookingInfo userBookingInfo) {
this.userBookingInfo = userBookingInfo;
}
public UserPhotoList getUserPhotoList() {
return userPhotoList;
}
public void setUserPhotoList(UserPhotoList userPhotoList) {
this.userPhotoList = userPhotoList;
}
}
Bean类UserBookingInfo.java
public class UserBookingInfo {
@Element(name="ArrivalDate",required=false)
private String arrivalDate;
@Element(name="CourseTypeCode",required=false)
private String courseTypeCode;
public String getArrivalDate() {
return arrivalDate;
}
public void setArrivalDate(String arrivalDate) {
this.arrivalDate = arrivalDate;
}
public String getCourseTypeCode() {
return courseTypeCode;
}
public void setCourseTypeCode(String courseTypeCode) {
this.courseTypeCode = courseTypeCode;
}
}
UserPhotoList列表中的Bean类(UserPhotoList.java)
@Root(name="UserPhotoList")
public class UserPhotoList {
@ElementList(inline=true,entry="UserPhoto")
private List<UserPhoto> userPhotos;
public List<UserPhoto> getUserPhotos() {
return userPhotos;
}
public void setUserPhotos(List<UserPhoto> userPhotos) {
this.userPhotos = userPhotos;
}
}
UserPhoto.java的Bean类
@Root
public class UserPhoto {
@Element(name = "LikeCount", required = false)
private String likeCount;
@Element(name = "UserPhotoId ", required = false)
private String userPhotoId ;
public String getLikeCount() {
return likeCount;
}
public void setLikeCount(String likeCount) {
this.likeCount = likeCount;
}
public String getUserPhotoId() {
return userPhotoId;
}
public void setUserPhotoId(String userPhotoId) {
this.userPhotoId = userPhotoId;
}
}
请让我在哪里做错了。非常感谢任何帮助。
当我使用simpleframe和Httpget
时,它工作正常以下是不使用RestTemplate的工作代码
new Thread(new Runnable(){
@Override
public void run() {
String xmlData = retrieve(url);
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
try {
UserProfile profile = serializer.read (UserProfile.class,
reader, false);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Error Occured",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} })开始();
public String retrieve(String url){
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
if (getResponseEntity != null) {
return EntityUtils.toString(getResponseEntity);
}
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
如何使用RestTemplete
实现相同的目标我在几天内遇到了问题,任何帮助都是非常有用的
答案 0 :(得分:0)
您的服务器返回XML内容,但表示它根据错误消息返回HTML内容(内容类型为text/html
),因此解析失败。您需要确保服务器返回类似text/xml
的内容,并确保您在其余模板对象中具有正确的转换器。
修改:尝试添加此消息转换器。 将其放在第一位(StringHttpMessageConverter
和SourceHttpMessageConverter
之前)
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters .add(jaxbMessageConverter);