简体   繁体   English

Java Android Spring:找不到适合请求类型的HttpMessageConverter

[英]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: 我一直在尝试使用Spring的RestTemplate将心跳从我的Android应用发布到Web服务,并且不断出现以下错误:

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 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: 这是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;
    }
}

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

暂无
暂无

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

相关问题 Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter - Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type 无法写入请求:找不到适合请求类型和内容类型的HttpMessageConverter [application / x-java-serialized-object] - Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object] RestTemplate无法编写请求:找不到适合请求类型[java.util.HashMap]的HttpMessageConverter - RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] 找不到适合请求类型 [java.util.LinkedHashMap] 和内容类型 [multipart/form-data] 的 HttpMessageConverter - no suitable HttpMessageConverter found for request type [java.util.LinkedHashMap] and content type [multipart/form-data] RestClientException:无法写入请求:找不到适合请求类型的HttpMessageConverter - RestClientException: Could not write request: no suitable HttpMessageConverter found for request type java 中的 HTTP POST 引发错误 - 找不到合适的 HttpMessageConverter 将请求正文读取到 object02ABBBA2F2A298EBDCC4 类型 - HTTP POST in java throws an error - no suitable HttpMessageConverter found to read request body into object of type class 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type HttpMessageConverter 异常:RestClientException:无法写入请求:找不到合适的 HttpMessageConverter - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found 没有为响应类型找到合适的HttpMessageConverter - Custom RestTemplate - No suitable HttpMessageConverter found for response type - Custom RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM