无法启动活动,无法创建转换器:使用翻新

时间:2019-10-31 15:10:27

标签: android gson retrofit2

我正在尝试使用GET请求调用api,但出现错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.movino, PID: 20106
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.movino/com.example.movino.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.example.movino.MoviesResponse
        for method TMBDApi.getPopular
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.example.movino.MoviesResponse
        for method TMBDApi.getPopular
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:115)
        at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:82)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:37)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:149)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy0.getPopular(Unknown Source)
        at com.example.movino.MoviesRepository.getMovies(MoviesRepository.java:38)
        at com.example.movino.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
            ... 11 more
     Caused by: java.lang.IllegalArgumentException: class com.example.movino.Movie declares multiple JSON fields named title
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:64)
        at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
        at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
        at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:113)
            ... 23 more

这是代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

    private RecyclerView moviesList;
    private MovieAdapter adapter;

    private MoviesRepository moviesRepository;

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

        moviesRepository = MoviesRepository.getInstance();

        moviesList = findViewById(R.id.recycler_view);
        moviesList.setLayoutManager(new LinearLayoutManager(this));

        moviesRepository.getMovies(new OnGetMoviesCallBack() {
            @Override
            public void onSuccess(List<Movie> movies) {
                adapter = new MovieAdapter(movies);
                moviesList.setAdapter(adapter);
            }

            @Override
            public void onError() {
                Toast.makeText(MainActivity.this, "Please check your internet connection.", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

MovieRepository:

public class MoviesRepository {

    public final static String BASE_URL="Base_Url";
    public final static String LANGUAGE="en-US";

    public static MoviesRepository repository;

    private TMBDApi api;

    private MoviesRepository(TMBDApi api){
        this.api=api;
    }

    public static MoviesRepository getInstance(){
        if(repository==null){
            Retrofit retrofit= new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            repository=new MoviesRepository(retrofit.create(TMBDApi.class));
        }

        return repository;
    }

    public void getMovies(final OnGetMoviesCallBack callBack){
        api.getPopular("api_key",LANGUAGE,1)
                .enqueue(new Callback<MoviesResponse>() {
                    @Override
                    public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {
                        if(response.isSuccessful()){
                            MoviesResponse moviesResponse=response.body();
                            if(moviesResponse!=null && moviesResponse.getMovies()!=null){
                                callBack.onSuccess(moviesResponse.getMovies());
                            }
                            else callBack.onError();
                        }
                        else callBack.onError();
                    }

                    @Override
                    public void onFailure(Call<MoviesResponse> call, Throwable t) {
                        callBack.onError();
                    }
                });
    }

}

MovieResponse:

public class MoviesResponse {

    @SerializedName("page")
    @Expose
    private int page;

    @SerializedName("total_results")
    @Expose
    private int totalResults;

    @SerializedName("results")
    @Expose
    private List<Movie> movies;

    @SerializedName("total_pages")
    @Expose
    private int totalPages;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(int totalResults) {
        this.totalResults = totalResults;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
}

使用Gson Converter之后,我仍然收到此错误。在这里,我通过MainActivity调用MovieRepository类。在MovieReposirtory中,我们定义存储库。之后,我们使用api变量获取响应。我不明白该错误。我是api调用的新手,所以我真的需要了解的解决方案。

0 个答案:

没有答案