Mathias Mathias - 3 months ago 69
Android Question

java android spring: no suitable HttpMessageConverter found for request type

I've been trying to post heartbeats from my Android app to a web service using Spring's RestTemplate and I keep getting the following error:

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


Here is where I use the 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);


Here is the HeartBeat class:

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;
}
}

Answer

This fixed my problem

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