【问题标题】:Spring Android Get Request implementation : Could not extract response: no suitable HttpMessageConverter found for response type and content typeSpring Android Get Request 实现:无法提取响应:没有找到适合响应类型和内容类型的 HttpMessageConverter
【发布时间】:2013-07-05 13:35:56
【问题描述】:

我正在尝试实现 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> 

注意:-

  1. UserProfile(Userprofile.java) 是根类或节点
  2. UserBookingInfo(UserBookingInfo.java)UserProfile 中的元素。
  3. 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 的工作代码

新线程(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();
            }

} }).start();

公共字符串检索(字符串网址){

    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 实现相同的目标

这几天我遇到了问题,非常感谢任何帮助

【问题讨论】:

    标签: android spring web-services resttemplate


    【解决方案1】:

    您的服务器返回一个 XML 内容,但说它返回 HTML 内容(根据错误消息,内容类型是 text/html),因此解析失败。您需要确保您的服务器返回类似 text/xml 的内容,并且您的 rest 模板对象中有正确的转换器。

    编辑:尝试添加此消息转换器。 放在第一位(在StringHttpMessageConverterSourceHttpMessageConverter 之前)

    Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.TEXT_HTML);
    jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
    messageConverters .add(jaxbMessageConverter);
    

    【讨论】:

    • 良好的观察,感谢您的反馈。我无法控制服务器返回的内容。您能否建议我在客户端进行任何工作。我可以使用 simpleFramework 和通过 Httpget 来实现(解析)相同的功能,但我只有使用 RestTemplate 才会遇到这个问题。我用使用 simpleFramework 但没有 RestTemplate 的工作代码更新我的问题。
    • 如果通过浏览器调用url会怎样?
    • 感谢您的跟进和建议,我添加了您的代码导致找不到类错误(Jaxb2RootElementHttpMessageConverter.class)我添加了特定的 jar 来修复,它导致我遇到其他问题,它继续.但是您的第一个建议是正确的,它是服务器端的问题,最后服务器人员在与他们讨论后解决了问题,现在一切正常,没有任何更改。我想尝试您在您的文章中提到的替代解决方案有时编辑(再一次)。再次感谢您。
    猜你喜欢
    • 2020-02-12
    • 2014-09-28
    • 2021-11-18
    • 2018-08-18
    • 2014-09-03
    • 2019-09-09
    • 2017-01-22
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多