Helenesh
Helenesh

Reputation: 4359

Retrofit 2: error in my GET request

I'm developing an Android app that shows movie posters for top rated movies. I want to build this by using Retrofit 2. However, I keep getting the error 'Service methods cannot return void'. Here's the stack trace:

FATAL EXCEPTION: main
Process: com.hfad.popularmovies, PID: 17921
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.popularmovies/com.hfad.popularmovies.MainActivity}: java.lang.IllegalArgumentException: Service methods cannot return void.
for method MoviesAPI.getFeedPopular
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: Service methods cannot return void.

I have created a POJO containing the fields I need & the necessary interface containing the get request =>

public interface MoviesAPI {
    @GET("/movie/top_rated?api_key=XXXXXXX")
    void getFeedTopRated(Callback <List<Movie>> response);

    @GET("/movie/popular?api_key=XXXXXXX")
    void getFeedPopular(Callback<List<Movie>> response);
}

Here's the fragment that should display the posters through a RecyclerView:

public class TopRatedFragment extends Fragment {
    private static final String ENDPOINT = "http://api.themoviedb.org/3/";
    private PosterAdapter adapter;
    private List<Movie> movieList;

    public TopRatedFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_top_rated, container, false);
        adapter = new PosterAdapter(getActivity());
        recyclerView.setAdapter(adapter);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(layoutManager);
        getTopRatedMovies();

        return recyclerView;
    }

    private void getTopRatedMovies() {
        Retrofit retrofit = new Retrofit.Builder()
                           .baseUrl(ENDPOINT)
                           .build();
        MoviesAPI api = retrofit.create(MoviesAPI.class);
        api.getFeedTopRated(new Callback<List<Movie>>() {
        @Override
        public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
            movieList = response.body();
            for (Movie movie : movieList) {
                movie.getPoster_path(); //I get the same error when I use 
            }
        }

        @Override
        public void onFailure(Call<List<Movie>> call, Throwable t) {
        }
    });
  }
}

Please, let me know if I should provide more code or background information.

Upvotes: 0

Views: 1226

Answers (1)

DZDomi
DZDomi

Reputation: 1679

Your interface method needs to return an object from the type

Call<Expected Result Object>

For example

@GET("/movie/top_rated?api_key=XXXXXXX")
Call<List<Movie>> getFeedTopRated();

And in the method you need to write the function as following:

api.getFeedTopRated().enque(new Callback<List<Movie>>() { /* */ });

Upvotes: 1

Related Questions