简体   繁体   中英

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. But the object is always null(no data stored)

  • I have double checked the server and it is working fine
  • the server returns {"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"}

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]

Here is the code that parses the JSON response

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

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. 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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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