0

Собственно, ошибка, как у всех, делаю как в ответах здесь, но работает неправильно.

Код с сайта:
[
  {
    "userId": 1,
    "id": 1,
    "title": "quidem molestiae enim"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "sunt qui excepturi placeat culpa"
  },
  {
    "userId": 1,
    "id": 3,
    "title": "omnis laborum odio"
  }
]
public class Album {
    @SerializedName("id")
    @Expose
    private int id;

    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("userId")
    @Expose
    private int userId;

//getters, setters...
}
public interface APIService {
    @POST("albums/")
    Call<List<Album>> loadAlbums();
}

------------------код запуска ретрофит---------------------
           final Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://jsonplaceholder.typicode.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            final APIService apiService = retrofit.create(APIService.class);
            Call<List<Album>> listCall = apiService.loadAlbums();
            listCall.enqueue(new Callback<List<Album>>() {
                @Override
                public void onResponse(Call<List<Album>> call, Response<List<Album>> response) {
                    if (response.isSuccessful()) {
                        DummyContent.ITEMS = response.body();
                        recyclerView.getAdapter().notifyDataSetChanged();
                        Log.d("OK", "responce is Successful");
                    }
                }

                @Override
                public void onFailure(Call<List<Album>> call, Throwable t) {
                    Log.e("failure", "Something wrong " + t.getMessage());
                }
            });

Так происходит onFailure, ещё пытаюсь сделать так:

public class AlbumList {
    List<Album> array;

    public List<Album> getAlbums() {
        return array;
    }

    public void setAlbums(List<Album> albums) {
        this.array = albums;
    }
}



public interface APIService {
    @POST("albums/")
    Call<AlbumList> loadAlbums();
}

В этом случае, array всегда null... В чём проблема? Как поправить ошибку? Спасибо.

1 ответ 1

0

Как оказалось, использовал не тот тип запроса - надо было GET, а я POST)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.