我正在使用 Retrofit2 和 Okhttp 在我的项目中进行 HTTP 调用,该调用也使用 Selenium。一旦我添加了以下依赖项:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.9.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我开始看到以下错误:
java.lang.NoSuchFieldError: Companion
at okhttp3.logging.HttpLoggingInterceptor$Logger$Companion$DefaultLogger.log(HttpLoggingInterceptor.kt:116)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:168)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
Run Code Online (Sandbox Code Playgroud)
我检查了一些相关问题,一切都指向依赖问题。但是,我使用的是 Okhttp 和 Retrofit2 (2.9.0) 以及 selenium-java 的最新版本:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我想要做的只是使用以下方法记录一个简单的请求:
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(url)
.client(httpClient.build())
.build();
Run Code Online (Sandbox Code Playgroud)
你知道可能是什么问题吗?
我在 IntelliJ 中使用最新的 Kotlin 插件
这很可能是依赖性问题。查看 IDE 中导入了哪些 JAR,然后调试错误。否则,创建一个简单的独立 Android 项目来运行此代码。您还可以使用 okhttp bom 干净升级到 4.9.0。
https://github.com/square/okhttp#releases
dependencies {
// define a BOM and its version
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
// define any required OkHttp artifacts without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3610 次 |
| 最近记录: |