我正在尝试实现 Android spring get 请求。当我运行应用程序时,出现以下错误。
无法提取响应:没有找到适合响应类型 [com.example.userprofiledemo.UserProfile] 和内容类型 [text/html;charset=UTF-8] 的 HttpMessageConverter
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)
是根类或节点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(UserPhotoList.java) List 的 Bean 类
@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 的工作代码
新线程(新可运行(){
@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();
}
} })。开始();
公共字符串检索(字符串网址){
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 实现相同的目标
我从几天开始就遇到了问题,任何帮助都非常有用