【问题标题】:Why do I get "Type okhttp3.Call does not have type parameters" when using Retrofit2?为什么我在使用 Retrofit2 时得到“Type okhttp3.Call 没有类型参数”?
【发布时间】:2016-05-12 14:57:43
【问题描述】:

我正在尝试遵循 Retrofit2 Getting Started and Create an Android Client 的本教程。

进口很好

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

除了一件事,我可以很好地按照教程进行操作。我正在尝试创建GitHubService Interface,但遇到了两个问题:Call<V> 表示它不接受任何类型参数,而且我也不确定将Contributor 类放在哪里,因为它仅根据教程声明为static,这是否意味着它嵌套在某个地方?

import okhttp3.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface GitHubClient {
    @GET("/repos/{owner}/{repo}/contributors")
    Call<List<Contributor>> contributors(
        @Path("owner") String owner,
        @Path("repo") String repo
    );
}

static class Contributor {
    String login;
    int contributions;
}

我将 Contributor 类放在一个单独的文件中,并将其公开。 另外,Call 类不会在 Android Studio 中自动导入,我必须手动选择它,但它是我得到的唯一 Call(Android 手机 api 除外)

请帮助我了解为什么会出现此错误,据我所知,周围没有人有相同的东西,所以我错过了一些基本的东西。

【问题讨论】:

    标签: java android android-studio retrofit2 okhttp3


    【解决方案1】:

    根据您得到的编译时错误,您确实从错误的包中导入了Call。请检查您的导入并确保您有

    import retrofit2.Call;
    

    所有与改造相关的导入都应该来自包retrofit2

    另一方面

     Call contributors(
    

    它无法猜测你想要返回什么。一个ContributorList&lt;Contributor&gt; 也许?例如

    public interface GitHubClient {
        @GET("/repos/{owner}/{repo}/contributors")
        Call<List<Contributor>> contributors(
            @Path("owner") String owner,
            @Path("repo") String repo
        );
    }
    

    【讨论】:

    • 我将其更改为您编写的确切内容(与教程相同),现在我收到上述错误
    • 改造中没有Call 可用:S 如果我选择呼叫并按alt+enter,我只能选择创建新课程等。
    • 我需要导入旧的 Retrofit 版本吗?如果我 compilecom.squareup.retrofitcom.squareup.retrofit2 都可以导入 retrofit.Call。这是我应该做的吗?
    • 不,你不需要那个。你只需要retrofit2,你必须导入this
    • 我确实编辑了答案。如果仍有问题,请发布更多代码
    猜你喜欢
    • 2017-08-24
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2017-05-03
    • 2020-09-20
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多