简体   繁体   English

改造不能得到json(android)

[英]Retrofit cant get json(android)

Sorry for my english. 对不起我的英语不好。 I try learned retrofit for android(before i use volley). 我尝试为android学习改造(在我使用凌空之前)。 And i dont know why its not work. 我不知道为什么它不起作用。

My class object PostsS 我的班级对象帖子

private String userId;
    private String id;
    private String title;
    private String body;

//the getters and setters

interface Links 界面链接

public interface Links {
    @GET("/posts")
    Call<PostsS> getPosts();
}

and my main 和我的主要

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .build();

        Links service = retrofit.create(Links.class);
        Call<PostsS> call = service.getPosts();

        call.enqueue(new Callback<PostsS>() {
            @Override
            public void onResponse(retrofit2.Response<PostsS> response) {
                Log.e("responce", response.toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("error", t.toString());
            }
        });

And i have 我有

Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.example.alexy.myapplication.PostsS
                                                     for method Links.getPosts

My json 我的json

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  }
]

With retrofit 2, you will have to specify explicitly the converter you want to use. 通过改造2,您必须明确指定要使用的转换器。 Eg 例如

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

will you Gson as converter. 你会Gson作为转换器。 You will have to add also the dependicy in your build.gradle file. 您还必须在build.gradle文件中添加build.gradle Eg 例如

compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

try to use the same -beta2 for everything retrofit2 related. 尝试使用相同的-beta2来解决所有与-beta2相关的问题。

Edit 编辑

Accordingly to the JSON you posted your method should return 根据您发布的JSON,您的方法应该返回

Call<List<PostsS>> instead of Call<PostsS> Call<List<PostsS>>而不是Call<PostsS>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM