简体   繁体   English

Spring Android Framework-通过HTTP GET检索JSON数据

[英]Spring Android Framework - Retrieving JSON data via HTTP GET

I am using Spring framework to get JSON data from a local server into an Object via Http GET. 我正在使用Spring框架通过Http GET将JSON数据从本地服务器获取到Object中。 But the object is always null(no data stored) 但是对象始终为null(不存储数据)

  • I have double checked the server and it is working fine 我已经仔细检查过服务器,并且工作正常
  • the server returns {"Propid":"61", "Proptitle":"3 bhk villa","Propdealer":"admin"} 服务器返回{"Propid":"61", "Proptitle":"3 bhk villa","Propdealer":"admin"}
  • I have added the Jackson Libraries 我已经添加了杰克逊图书馆
  • I have used StringHttpMessageConverter and it returns the JSON string {"Propid":"61", "Proptitle":"3 bhk villa","Propdealer":"admin"} 我使用了StringHttpMessageConverter,它返回JSON字符串{"Propid":"61", "Proptitle":"3 bhk villa","Propdealer":"admin"}

Throws exception: Could not extract response: no suitable HttpMessageConverter found for response type [com.aditya.master.classes.Prop] and content type [text/html;charset=UTF-8] 引发异常: Could not extract response: no suitable HttpMessageConverter found for response type [com.aditya.master.classes.Prop] and content type [text/html;charset=UTF-8]

Here is the code that parses the JSON response 这是解析JSON响应的代码

URI targetUrl= UriComponentsBuilder.fromUriString("http://192.168.1.9/PinSpace/oauth/")
                    .path("request_access/")
                    .queryParam("query", "get_property")
                    .queryParam("access_token", auth_code)
                    .queryParam("prop_id", "61")
                    .build()
                    .toUri();

            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
            HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);


            RestTemplate restTemplate = new RestTemplate();

            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());


            ResponseEntity<Prop> responseEntity = restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, Prop.class);
            Prop result = responseEntity.getBody();

Here is the Prop class 这是道具课

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Prop {

@JsonProperty
private String Propid, Proptitle, Propdealer;


public String getPropid() {
    return Propid;
}

public void setPropid(String propid) {
    Propid = propid;
}

public String getProptitle() {
    return Proptitle;
}

public void setProptitle(String proptitle) {
    Proptitle = proptitle;
}

public String getPropdealer() {
    return Propdealer;
}

public void setPropdealer(String propdealer) {
    Propdealer = propdealer;
}
}

Please suggest a solution 请提出解决方案

Thanks! 谢谢!

You can test deserialization with follow code: 您可以使用以下代码测试反序列化:

ObjectMapper objectMapper = new ObjectMapper();
        String content = "{\"Propid\":\"61\", \"Proptitle\":\"3 bhk villa\",\"Propdealer\":\"admin\"}";
        objectMapper.readValue(content , Prop.class);

This trows exeception 这引发了观念

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Propid"

which means that fields naming in your class is incorrect or you need to point correct names in @JsonProperty annotation 这意味着您的班级中的字段命名不正确,或者您需要在@JsonProperty批注中指向正确的名称

I suggest you to use next structure: 我建议您使用下一个结构:

public class Prop {

    private String propid;
    private String proptitle;
    private String propdealer;

    public String getPropid() {
        return propid;
    }

    @JsonProperty("Propid")
    public void setPropid(String propid) {
        this.propid = propid;
    }

    public String getProptitle() {
        return proptitle;
    }

    @JsonProperty("Proptitle")
    public void setProptitle(String proptitle) {
        this.proptitle = proptitle;
    }

    public String getPropdealer() {
        return propdealer;
    }

    @JsonProperty("Propdealer")
    public void setPropdealer(String propdealer) {
        this.propdealer = propdealer;
    }

}

There is a way to get this to work with an incorrect MIME type as well: you just need to add "text/html" to your list of accepted media types. 也有一种方法可以使它与不正确的MIME类型一起使用:您只需要在接受的媒体类型列表中添加“ text / html”即可。 like so: 像这样:

MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
List<MediaType> mediaTypeList = new ArrayList<MediaType>();
//...
mediaTypeList.addAll( jsonConverter.getSupportedMediaTypes() );
mediaTypeList.add(MediaType.TEXT_HTML);
jsonConverter.setSupportedMediaTypes(mediaTypeList);

this will be quite handy if you don't have access to the server. 如果您无权访问服务器,这将非常方便。

NOTE 注意

there's probably a less verbose way to do this, but I'm just getting back to Java after 10 years in other environs :-) 可能没有那么冗长的方法了,但是在其他环境中工作了十年后,我才回到Java :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM