0

I create a simple application on android which is a search engine movies. It uses GSON + Retrofit.

In class FilmApiRequester have error.

Api.getFilmInfo (apikey, filmTitle, pageLimit, callback);

Non-static method 'getFilmInfo (java.lang.String, java.lang.String, java.lang.String, retrofit.Callback )' can not be referenced from a static context.

I do not know how to solve the problem.

FilmApiRequester.class

public class FilmApiRequeter {


private static String ROOT ="http://example.com";

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(ROOT)
        .build();
Api filmRequester = restAdapter.create(Api.class);

private String apikey="ApiKeyExample";

public void getFilms(String filmTitle,String pageLimit, Callback<Film> callback)
{
    Api.getFilmInfo(apikey,filmTitle,pageLimit,callback);
} }

Api.class

public interface Api{

@GET("/movies.json")
public void getFilmInfo(@Query("apikey") String apikey, @Query("q") String filmTitle, @Query("page_limit") String pageLimit, Callback<Film> callback); }
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
Se-BASS-tian
  • 51
  • 1
  • 7
  • See http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html: *Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.* – Emil Lundberg Jan 27 '15 at 10:19
  • possible duplicate of ["Non-static method cannot be referenced from a static context" error](http://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – Emil Lundberg Jan 27 '15 at 10:22

1 Answers1

2

Your problem is here :

public void getFilms(String filmTitle,String pageLimit, Callback<Film> callback)
{
    Api.getFilmInfo(apikey,filmTitle,pageLimit,callback);  /// this line
}

You have created your method as :

public interface Api{

@GET("/movies.json")
public void getFilmInfo

Since your method getFilmInfo is a non-static, you cannot call it without an instance. So either make it static or create an instance of a class that implements your interface Api.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • I edited the code will work correctly? `public void getFilms(String filmTitle,String pageLimit, Callback callback) { Api api = new Api() { @Override public void getFilmInfo(@Query("apikey") String apikey, @Query("q") String filmTitle, @Query("page_limit") String pageLimit, Callback callback) { } }; api.getFilmInfo(apikey,filmTitle,pageLimit,callback); }` – Se-BASS-tian Jan 27 '15 at 13:04
  • just add `static` keyword to your method like `public static void getFilmInfo` – Abubakkar Jan 27 '15 at 14:03