Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android RestClientException: no suitable HttpMessageConverter found

I am using spring android rest template and trying to convert a JSON response into a java class as shown here.

Here is my snippet trying to do the work:

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    //I have already tried to manually setting converters with no luck
    //List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    //messageConverters.add(new MappingJacksonHttpMessageConverter());
    //messageConverters.add(new FormHttpMessageConverter());
    //messageConverters.add(new StringHttpMessageConverter());
    //restTemplate.setMessageConverters(messageConverters);

    Result result = restTemplate.getForObject(url, Result.class);

I think I have done everything required as it is told in the document:

  • I have following jars in my classpath
    • jackson-all-1.7.6.jar
    • spring-android-rest-template-1.0.0.M2.jar
  • the url really returns a JSON object.
  • My Result class is a POJO with all the required fields

Basically having the Jackson dependencies in my classpath must be enough to get it work as it says:

The MappingJacksonHttpMessageConverter is conditionally loaded when you create a new RestTemplate instance. If the Jackson dependencies are found in your classpath, the message converter will be automatically added and available for use in REST operations.

So what I am missing here, what I am doing wrong?

Thanks

Full exception can be found here:

04-20 04:25:52.706: ERROR/AndroidRuntime(9638): FATAL EXCEPTION: main
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): java.lang.IllegalStateException: Could not execute method of the activity
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.view.View$1.onClick(View.java:2083)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.view.View.performClick(View.java:2421)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.view.View$PerformClick.run(View.java:8869)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.os.Handler.handleCallback(Handler.java:587)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.os.Looper.loop(Looper.java:143)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.app.ActivityThread.main(ActivityThread.java:5068)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at java.lang.reflect.Method.invoke(Method.java:521)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at dalvik.system.NativeStart.main(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): Caused by: java.lang.reflect.InvocationTargetException
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at xxx.SamplePageActivity.doRest(SamplePageActivity.java:83)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at java.lang.reflect.Method.invoke(Method.java:521)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at android.view.View$1.onClick(View.java:2078)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     ... 11 more
04-20 04:25:52.706: ERROR/AndroidRuntime(9638): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [[Lxxx.SamplePageActivity$Result;] and content type [application/json;charset=UTF-8]
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:77)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:202)
04-20 04:25:52.706: ERROR/AndroidRuntime(9638):     ... 15 more
like image 833
dds Avatar asked Apr 20 '11 01:04

dds


1 Answers

I've never done anything on Android but I know converts are picky with Content-Type. For Jackson to work the message body needs to have 'Content-Type: application/json'. Try to check all headers on the response.

like image 98
Andreas Wederbrand Avatar answered Oct 20 '22 03:10

Andreas Wederbrand