Search code examples
androidretrofit2

Retofit create request inside object


I am trying to get data from: https://yts.lt/api/v2/list_movies.json

Here is my code:

public class ApiClient {


    private final static String DOMAIN = "https://yts.lt/api";
    private final static String API = "/v2/list_movies.json/";

    private final static String BASE_URL = String.format("%s%s", DOMAIN, API);


    private static Retrofit retrofit = null;

    //Retrofit
    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}


public interface ApiService {

    //how to create the GET?

    @GET("all")
     Single<List<Movie>> getAllMovies();
}

Main Activity:

public class MainActivity extends AppCompatActivity {

    private ApiService apiService;
    private CompositeDisposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDisposable = new CompositeDisposable();
        apiService = ApiClient.getInstance().create(ApiService.class);


        mDisposable.add(apiService.getAllMovies().
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribeWith(new DisposableSingleObserver<List<Movie>>() {


                    @Override
                    public void onSuccess(final List<Movie> movies) {


                        Log.d(TAG,"Sucess");

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, String.format("onError: %s", e.getMessage()), e);

                    }
                })
        );
    }
}

Movie:

public class Movie implements Serializable {

    @SerializedName("title")
    private String title;


    public String getNameTitle() {
        return title;
    }


}

Error:

E/DTAG: onError: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

The movies array is located in object►data►movies► And I need to get it? can you please help me how to create the @GET


Solution

  • Your endpoint return type is not well structured. It should have the same structure as the endpoint response. You should create the following classes:

    
    class MoviesResponse {
       @SerializedName("data")
       MoviesData data;
       ...
    }
    
    class MoviesData {
       @SerializedName("movies")
       List<Movie> data;
       ...
    }
    
    class Movie {
       @SerializedName("title")
       String title;
       ...
    }
    

    Note that you should add the properties you want in each class, for example, in MoviesResponse you could also add status field as it is in the JSON response.

    Then change your service to have as return type the MoviesResponse

    public interface ApiService {
    
         @GET("all")
         Single<MoviesResponse> getAllMovies();
    }