改造:无法创建适配器

时间:2016-02-21 08:52:51

标签: java android retrofit

我正在使用改装进行HTTP调用。但是app会引发异常:

  

02-21 14:07:40.527 21157-21157 / com.popularmovies E / AndroidRuntime:   致命异议:主要                                                                                      过程:com.popularmovies,PID:21157                                                                                      java.lang.IllegalArgumentException:无法为其创建调用适配器   class com.entity.PopularMovies                                                                                          方法MovieDbAPI.getPopularMovies                                                                                          at retrofit.Utils.methodError(Utils.java:177)                                                                                          at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:47)                                                                                          at retrofit.MethodHandler.create(MethodHandler.java:26)                                                                                          at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)                                                                                          at retrofit.Retrofit $ 1.invoke(Retrofit.java:132)                                                                                          at $ Proxy1.getPopularMovies(Native Method)                                                                                          在   com.service.MovieServiceImpl.getPopularMovies(MovieServiceImpl.java:27)                                                                                          在   com.popularmovies.MainActivityFragment.getPopularMovies(MainActivityFragment.java:49)                                                                                          在   com.popularmovies.MainActivityFragment.onClick(MainActivityFragment.java:77)                                                                                          在android.view.View.performClick(View.java:4444)                                                                                          在android.view.View $ PerformClick.run(View.java:18457)                                                                                          在android.os.Handler.handleCallback(Handler.java:733)                                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                          在android.os.Looper.loop(Looper.java:136)                                                                                          在android.app.ActivityThread.main(ActivityThread.java:5113)                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                                                          在java.lang.reflect.Method.invoke(Method.java:515)                                                                                          在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)                                                                                          at dalvik.system.NativeStart.main(Native Method)                                                                                       引起:java.lang.IllegalArgumentException:找不到调用   com.entity.PopularMovies类的适配器。尝试:                                                                                       * retrofit.ExecutorCallAdapterFactory                                                                                          at retrofit.Retrofit.nextCallAdapter(Retrofit.java:207)                                                                                          at retrofit.Retrofit.callAdapter(Retrofit.java:175)                                                                                          at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:45)                                                                                             ......还有18个

public class MovieServiceImpl implements MovieService{

private static String baseURL =  "http://api.themoviedb.org/3/discover/movie/";

private static String sortBy = "popularity.desc";
private static String key ="*****";

@Override
public PopularMovies  getPopularMovies(){

  Retrofit retrofit = Util.getRetrofit(baseURL);

  MovieDbAPI movieAPI = retrofit.create(MovieDbAPI.class);

  PopularMovies popularMovies =  movieAPI.getPopularMovies( sortBy, key );

  return popularMovies;
}
}

public class Util {


public static Retrofit getRetrofit(String baseURL){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}


}



 public interface MovieDbAPI {

 @GET
 PopularMovies getPopularMovies(@Query("sort_by") String sort_by,   @Query("api_key") String key);

}

1 个答案:

答案 0 :(得分:1)

试试这个:

@GET
Call<PopularMovies> getPopularMovies(@Query("sort_by") String sort_by, @Query("api_key") String key);

当您致电getPopularMovies方法时,请按照以下方式进行:

同步请求

Call<PopularMovies> call = movieAPI.getPopularMovies(sortBy, key);
PopularMovies popularMovies = call.execute();

异步请求

Call<PopularMovies> call = movieAPI.getPopularMovies(sortBy, key);
call.enqueue(new Callback<PopularMovies>() {
        @Override
        public void onResponse(Response<PopularMovies> response) {

        }

        @Override
        public void onFailure(Throwable t) {
        }
    });