无法解析导入 com.google.api.client.json.gson.GsonFactory

Cannot resolve import com.google.api.client.json.gson.GsonFactory

我正在尝试在我的应用程序中使用 GsonFactory class:

StudentApi.Builder builder = new StudentApi.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), null);

但是它说 cannot resolve symbol 'GsonFactory'

我在 class

中导入了
import com.google.api.client.json.gson.GsonFactory;

但是gson无法解决所以我尝试了Alt-Enter - Find jar on web但是找不到库。

我的 build.gradle 依赖项中有这个:

dependencies {
   ...
   compile 'com.google.code.gson:gson:2.3'
   compile 'com.google.api-client:google-api-client-android:1.19.0'
}

我可以确认 class 确实 exist

您需要使用这个库:

compile 'com.google.http-client:google-http-client-gson:1.19.0'

在您的 gradle.build 中您需要添加:

compile ('com.google.http-client:google-http-client-gson:1.19.0') {
    exclude module: 'httpclient'
}

之后

compile('com.google.http-client:google-http-client-android:1.19.0') {
    exclude(group: 'com.google.android', module: 'android')
}

这样做之后,您将可以使用:

com.google.api.client.json.gson.GsonFactory()

此对象仅存在于旧版本的 google-http-client 中,尝试使用 1.9.0-beta 版本

compile 'com.google.http-client:google-http-client-android:1.9.0-beta'

GsonFactory 来自旧版本。

现在,您应该改用 JacksonFactory.getDefaultInstance()

像这样导入import com.google.api.client.json.jackson2.JacksonFactory;

自 2022 年起,GsonFactory 回归并弃用 JacksonFactory。将 JacksonFactory 替换为 GsonFactory.getDefaultInstance().

在Gradle中:使用

implementation 'com.google.http-client:google-http-client-gson:1.41.0'