1

Sorry for my english. I try learned retrofit for android(before i use volley). 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

[
  {
    "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"
  }
]
j2417706
  • 43
  • 1
  • 6

1 Answers1

3

With retrofit 2, you will have to specify explicitly the converter you want to use. E.g.

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

will you Gson as converter. You will have to add also the dependicy in your build.gradle file. E.g.

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

try to use the same -beta2 for everything retrofit2 related.

Edit

Accordingly to the JSON you posted your method should return

Call<List<PostsS>> instead of Call<PostsS>

Blackbelt
  • 156,034
  • 29
  • 297
  • 305