有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java改装调用包含错误要求改装2。呼叫,发现无效

你好,我最近在执行代码时启动了retrofit,我收到了这个错误

Incompatible types. 
Required: retrofit2.Call <java.util.List<com.my.package.Youtube.YoutubePost>>
Found: void

我试图使用Youtube v3 API获取YouTube频道播放列表

这是我的代码

YoutubeActivity

    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(ScalarsConverterFactory.create())
            .baseUrl(AppConstant.API_YT_BASE)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    YoutubeApiInterface youtubeApiInterface = retrofit.create(YoutubeApiInterface.class);

    Call<List<YoutubePost>> call = youtubeApiInterface.getPlayList().enqueue(new Callback<List<YoutubePost>>() {
        @Override
        public void onResponse(Call<List<YoutubePost>> call, Response<List<YoutubePost>> response) {
            if (response.isSuccessful()) {

            } else {

            }
        }

        @Override
        public void onFailure(Call<List<YoutubePost>> call, Throwable t) {
            t.printStackTrace();

        }
    });

YoutubePost

public class YoutubePost implements Parcelable {
    @SerializedName("items")
    private List<YoutubeItems> ytItems = new ArrayList<>();
    private String nextPageToken;

    public List<YoutubeItems> getYtItems() {
        return ytItems;
    }

    public String getNextPageToken() {
        return nextPageToken;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(ytItems);
        dest.writeString(nextPageToken);
    }

    protected YoutubePost(Parcel in) {
        ytItems = in.readParcelable(YoutubeItems.class.getClassLoader());
        nextPageToken = in.readString();
    }

    public static final Creator<YoutubePost> CREATOR = new Creator<YoutubePost>() {
        @Override
        public YoutubePost createFromParcel(Parcel source) {
            return new YoutubePost(source);
        }

        @Override
        public YoutubePost[] newArray(int size) {
            return new YoutubePost[size];
        }
    };
}

错误信息截图附在下面

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    我建议您稍微修改一下getPlayList方法,通过调用返回,然后断线。 没有你的YoutubeApiInterface我就只有这些了

    Call<List<YoutubePost>> call = youtubeApiInterface.getPlayList();
    call.enqueue(new Callback<List<YoutubePost>>() {
            @Override
            public void onResponse(Call<List<YoutubePost>> call, Response<List<YoutubePost>> response) {
                if (response.isSuccessful()) {
    
                } else {
    
                }
            }.
    
            @Override
            public void onFailure(Call<List<YoutubePost>> call, Throwable t) {
                t.printStackTrace();
    
            }
        });
    

    这样就不会有类型问题,因为在排队过程中,调用本身会被修改