java android spring:找不到合适的HttpMessageConverter请求类型

时间:2016-08-25 12:13:12

标签: java android spring

我一直试图使用Spring的RestTemplate将我的Android应用中的心跳发布到网络服务,并且我不断收到以下错误:

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [dk.actura.services.HeartBeat]

这是我使用RestTemplate的地方:

RestTemplate restTemplate = new RestTemplate();

// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

// Make the HTTP GET request, marshaling the response to a String
HeartBeat heartBeat = new HeartBeat();
return restTemplate.postForObject(url + "SendHeartBeat", heartBeat, String.class);

这是 HeartBeat 类:

public class HeartBeat {
    private int Id;
    private String SignalType;
    private long HeartBeatTime;
    private int UnitId;
    private short BatteryPercentage;
    private short SignalStrength;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getSignalType() {
        return SignalType;
    }

    public void setSignalType(String signalType) {
        SignalType = signalType;
    }

    public long getHeartBeatTime() {
        return HeartBeatTime;
    }

    public void setHeartBeatTime(long heartBeatTime) {
        HeartBeatTime = heartBeatTime;
    }

    public int getUnitId() {
        return UnitId;
    }

    public void setUnitId(int unitId) {
        UnitId = unitId;
    }

    public short getBatteryPercentage() {
        return BatteryPercentage;
    }

    public void setBatteryPercentage(short batteryPercentage) {
        BatteryPercentage = batteryPercentage;
    }

    public short getSignalStrength() {
        return SignalStrength;
    }

    public void setSignalStrength(short signalStrength) {
        SignalStrength = signalStrength;
    }

    public HeartBeat() {
        Id = -1;
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat();
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            date = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SignalType = "4G";
        HeartBeatTime = date.getTime();
        UnitId = 1;
        BatteryPercentage = 1;
        SignalStrength = 21;
    }

    public HeartBeat(String signalType, long heartBeatTime, int unitId, short batteryPercentage, short signalStrength) {
        SignalType = signalType;
        HeartBeatTime = heartBeatTime;
        UnitId = unitId;
        BatteryPercentage = batteryPercentage;
        SignalStrength = signalStrength;
    }
}

1 个答案:

答案 0 :(得分:0)

这解决了我的问题

RestTemplate restTemplate = new RestTemplate();
HeartBeat heartBeat = new HeartBeat(appPreferences.getInt("UnitId"), (short) bM.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY), (short) myPhoneStateListener.getSignalStrength(), mSignalType);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(new MediaType("application", "json", Charset.forName("UTF-8"))));
try {
    HttpEntity<HeartBeat> entityHeartBeat = new HttpEntity<>(heartBeat, httpHeaders);
    ResponseEntity<String> responseEntity = restTemplate.exchange(url + "SendHeartBeat",
                    HttpMethod.POST, entityHeartBeat, String.class);
    return responseEntity.getBody();
} catch (Exception e) {
    Log.e("SiteConnection", ">>>>>>>>>>>>>>>> " + e.getLocalizedMessage());
    e.printStackTrace();
}