找不到合适的HttpMessageConverter用于响应类型[com.enimbus.book.Post]和内容类型[text / html; charset = UTF-8]

时间:2016-11-02 09:51:39

标签: rest android-studio spring-boot

我正在使用restful服务开发Android应用程序。我从url调用get请求,它返回内容类型application / json; charset = UTF-8。我想在我的android视图中显示返回json数据。要做到这一点,我在android mainactivity中使用下面的代码

private class HttpRequestTask extends AsyncTask<Void, Void, Post> {
    @Override
    protected Post doInBackground(Void... params) {
        try {
            final String url = "http://192.168.0.100:8080/rposts/view/46";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Post post = restTemplate.getForObject(url, Post.class);
            return post;
        } catch (Exception e) {
            Log.e("MainActivity", e.getMessage(), e);
        }

        return null;

    }

    @Override
    protected void onPostExecute(Post post) {
        TextView PostIdText = (TextView) findViewById(R.id.post_title);
        TextView PostContentText = (TextView) findViewById(R.id.post_body);
        PostIdText.setText(post.getTitle());
        PostContentText.setText(post.getBody());
    }

}

当我运行我的应用时,它会出错

无法提取响应:没有为响应类型[com.enimbus.book.Post]和内容类型[text / html; charset = UTF-8]找到合适的HttpMessageConverter                                                                           org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[com.enimbus.book.Post]和内容类型[text / html; charset = UTF-8]找到合适的HttpMessageConverter                                                                               在org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:79)                                                                               在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)                                                                               在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)                                                                               在org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)                                                                               在com.enimbus.book.MainActivity $ HttpRequestTask.doInBackground(MainActivity.java:123)                                                                               在com.enimbus.book.MainActivity $ HttpRequestTask.doInBackground(MainActivity.java:116)                                                                               在android.os.AsyncTask $ 2.call(AsyncTask.java:288)                                                                               在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                               在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                               at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)                                                                               在java.lang.Thread.run(Thread.java:841) 11-02 14:34:39.194 27146-27146 / com.enimbus.book D / AndroidRuntime:关闭VM 11-02 14:34:39.194 27146-27146 / com.enimbus.book W / dalvikvm:threadid = 1:线程退出时未捕获异常(group = 0x41e90da0) 11-02 14:34:39.204 27146-27146 / com.enimbus.book E / AndroidRuntime:FATAL EXCEPTION:main                                                                             过程:com.enimbus.book,PID:27146                                                                             显示java.lang.NullPointerException                                                                                 在com.enimbus.book.MainActivity $ HttpRequestTask.onPostExecute(MainActivity.java:137)                                                                                 在com.enimbus.book.MainActivity $ HttpRequestTask.onPostExecute(MainActivity.java:116)                                                                                 在android.os.AsyncTask.finish(AsyncTask.java:632)                                                                                 在android.os.AsyncTask.access $ 600(AsyncTask.java:177)                                                                                 在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 在android.os.Looper.loop(Looper.java:146)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5653)                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)                                                                                 在java.lang.reflect.Method.invoke(Method.java:515)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)                                                                                 在dalvik.system.NativeStart.main(本地方法)

我在gradle app中使用以下依赖项

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'

}

当我用postman

测试时,

返回了json数据

{
"id": 46,
"title": "hellov",
"slug": "tharu",
"postedOn": "08/12/2016 3:04:58 PM",
"keywords": [
    "i"
],
"tags": [
    "love"
],
"active": true,
"author": {
    "id": 20,
    "firstName": "Tharindu",
    "lastName": "Gihan",
    "email": "gihan@gmail.com"
},
"teaser": "<p>to</p>",
"body": "<p>you</p>"

}

服务器端弹簧引导支架控制器

package com.gihangreen.controller.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gihangreen.domain.Post;
import com.gihangreen.service.CommentService;
import com.gihangreen.service.PostService;


@RestController
@RequestMapping("/rposts")
public class PostRestController {

 private PostService postService;
 private CommentService commentService;

@Autowired
public PostRestController(PostService postService, CommentService    commentService) {
    super();
    this.postService = postService;
    this.commentService = commentService;
}

//get all posts
@RequestMapping( value = "/list", method = RequestMethod.GET )
public Iterable<Post> list(){
    return postService.list();
}

//get post content by id
@RequestMapping(value="/view/{id}", method = RequestMethod.GET)
public Post read(@PathVariable(value = "id") long id) {
    return postService.get(id);
}

//get post by author id
@RequestMapping(value="/byAuthor/{id}", method = RequestMethod.GET)
public Iterable<Post> byAuthor(@PathVariable(value = "id") long id) {
    return postService.listByAuthor(id);

}

//search post by string
@RequestMapping(value="/search", method = RequestMethod.GET)
public Iterable<Post> search(@RequestParam("search") String search) {
    return postService.searching(search);

}


}

我是如何解决这个问题的?帮助

1 个答案:

答案 0 :(得分:0)

最好先看一下发回的数据是什么?

 CURL http://192.168.0.100:8080/rposts/view/46 -v