【问题标题】:Retrofit 2 Class File issue改造 2 类文件问题
【发布时间】:2019-07-22 00:07:06
【问题描述】:

我目前在我的应用程序中使用改造 2 时遇到问题。

public static Retrofit getClient(Context context)
{

    if (okHttpClient == null)
        initOkHttp(context);

    if (retrofit == null)
    {
        retrofit = new Retrofit.Builder()
                .baseUrl(CaselotREST.CASELOT_BASEURL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

当我尝试编译我的项目时出现以下错误。

错误:无法访问 Retrofit2 的 Retrofit 类文件。Retrofit not 找到了

【问题讨论】:

  • 您是否将所需的依赖项添加到您的 build.gradle 中?
  • @Phil 是的,但我必须将它们添加到我的 webapi gradle 文件中的应用程序 Gradle 文件中。

标签: java android retrofit2 rx-java2


【解决方案1】:

我想通了。问题源于我们 android 应用程序中代码库的模块化。我的 android 应用程序中有很多模块,每个模块都有依赖项。改造的依赖项,其中 Webapi 模块但不在主应用程序模块中。解决这个问题的方法是将以下内容添加到主 gradle 文件中

implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

【讨论】:

    【解决方案2】:

    如果使用 RxJava2 进行改造,你需要这样的东西。 为 gson 和改造添加依赖项,如果您在改造 api 服务中使用可观察的 rxjava,则适配器会导入一些东西。

    /* retrofit, gson */
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
    implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
    

    那么您的构建器将如下所示:

     Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_API_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
    

    在哪里,

    okHttpClient = new OkHttpClient.Builder().build();
    

    然后您可以创建服务,

    retrofit.create(RetrofitAPIService.class);
    

    您的 RetrofitAPIService 将包含类似于此的 API 实现:

    @Headers("Content-Type: application/json")
    @POST("v1/login/")
    Observable<ApiResponse> userLogin(
            @Body String body
    );
    

    【讨论】:

      【解决方案3】:

      确保您在项目的build.gradle 文件中添加了改造依赖项。

      implementation 'com.squareup.retrofit2:retrofit:2.5.0'
      

      【讨论】:

      • build.gradle 中的依赖关系是人们很容易错过的事情。去伟大的 Pooja。
      【解决方案4】:

      如果你之前使用Retrofit 1.x,现在继续使用Retrofit 2,你可以在你的gradle文件中添加这一行

      implementation 'com.squareup.retrofit2:retrofit:2.5.0'
      

      还要确保您对 AndroidManifest 文件具有所需的权限

        <uses-permission android:name="android.permission.INTERNET" />
      

      如果您对使用RXJavaRetrofit 感兴趣,可以查看此link 以获取教程。

          //example 
          Gson gson = new GsonBuilder()
                  .setLenient()
                  .create();
      
          Retrofit build = new Retrofit.Builder()
                  .baseUrl(BuildConfig.BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .client(unsafeOkHttpClient)
                  .build();
      

      如果这对你有帮助,那就干杯。

      【讨论】:

        猜你喜欢
        • 2018-04-13
        • 1970-01-01
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        相关资源
        最近更新 更多