Robospice上传文件问题

时间:2013-04-02 10:53:21

标签: android robospice

我是RoboSpice的新手。 我正在尝试上传文件。但我得到了这个错误:

 04-02 17:47:31.151: E//RequestProcessor.java:234(11021): org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/html]

。这是我的请求类:

public class UploadFileRequest extends SpringAndroidSpiceRequest<String>{
private static final String TAG = "UploadFileRequest";
private UploadRequestModel requestModel;
private String link;

public UploadFileRequest(UploadRequestModel model, String link) {
    super(String.class);
    requestModel = model;
    this.link = link;
}

@Override
public String loadDataFromNetwork() throws Exception {    


    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("file1", new FileSystemResource(requestModel.getFile1()));
    parts.add("file2", new FileSystemResource(requestModel.getFile1()));
    return getRestTemplate().postForObject(link, parts, String.class);


}

}

我正在使用:JacksonSpringAndroidSpiceService类。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

正如您从JacksonSpringAndroidSpiceService source code所看到的,它仅通过application/json类为MappingJacksonHttpMessageConverter内容类型提供转换器。

对于任何其他内容类型,Spring不知道如何处理它。

您可以使用StringHttpMessageConverter类子类JacksonSpringAndroidSpiceService轻松添加常规pourpose转换器,或者在源代码上创建自己的实现库。

答案 1 :(得分:2)

我遇到了类似的问题并通过以下

解决了这个问题

替换

return getRestTemplate().postForObject(link, parts, String.class);

RestTemplate restTemplate = getRestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate.postForObject(link, parts, String.class);

希望这有帮助!