Y
Y
yarovoi_ivan2016-05-20 18:41:04
Java
yarovoi_ivan, 2016-05-20 18:41:04

How to take nested object from response.body() for further work?

here is the json
BODY structure

{
  "keyword": "Звездные",
  "pagesCount": 15,
  "searchFilms": [
    {
      "id": "714888",
      "type": "KPFilm",
      "nameRU": "Звёздные войны: Пробуждение силы",
      "nameEN": "Star Wars: Episode VII - The Force Awakens",
      "description": "США, Джей Джей Абрамс(фантастика)",
      "posterURL": "film_iphone/iphone60_714888.jpg",
      "filmLength": "2:16",
      "year": "2015",
      "country": "США",
      "genre": "фантастика, фэнтези, боевик",
      "rating": "7.3 (97 627)"
    },
...
}

private void loadJSON(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://api.learn2crack.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    final RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

            Log.d(TAG, "response.body()");

            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getMuvi()));

            Log.d(TAG, data.toString());

            adapter = new DataAdapter(data);
            recyclerView.setAdapter(adapter);
        }
        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.d("Error",t.getMessage());
        }
    });
    }

but in response.body(); everything is fine there, but I need the "searchFilms" object and continue to work with it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kanesy, 2016-05-23
@kanesy

1. First you create the SearchFilms class with the corresponding fields.
2. Then you create the BaseResponse class which will have the SearchFilms field.
3. Convert response.body() to the BaseResponse you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question