retrofit2 - 是什么导致了这个 Retrofit2 异常 Unable to create call adapter for retrofit2.Response
问题描述
我正在尝试在 eclipse 中设置一个简单的 java 项目来测试 Retrofit2
我的 Api 界面类似于:-
import java.util.Map;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.QueryMap;
interface Api {
@Headers({ "X-XXX-APIKey: 92347592837529357323945", "Accept: application/json" })
@GET("content/search/simple")
Response<String> search(@QueryMap Map<String, String> options);
}
我的服务类类似于:-
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class Service implements Api {
private final HttpLoggingInterceptor logging;
private final Retrofit retrofit;
private final Api service;
{
logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging)
.connectTimeout(60000L, TimeUnit.MILLISECONDS).callTimeout(60000L, TimeUnit.MILLISECONDS)
.readTimeout(60000L, TimeUnit.MILLISECONDS).writeTimeout(60000L, TimeUnit.MILLISECONDS)
.followRedirects(true).followSslRedirects(true).build();
retrofit = new Retrofit.Builder().client(client).addConverterFactory(ScalarsConverterFactory.create()).baseUrl("https://api.my.org/").build();
service = retrofit.create(Api.class);
}
@Override
public Response<String> search(final Map<String, String> options) {
return service.search(options);
}
}
当我调用我的搜索方法时,我收到此异常:-
Exception in thread "main" java.lang.IllegalArgumentException: Unable to create call adapter for retrofit2.Response<java.lang.String>
for method Api.search
at retrofit2.Utils.methodError(Utils.java:54)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:116)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
at com.aaa.bbb.ccc.$Proxy0.search(Unknown Source)
at com.aaa.bbb.ccc.Service.search(Service.java:39)
at com.aaa.bbb.ccc.Manager.main(Manager.java:15)
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for retrofit2.Response<java.lang.String>.
Tried:
* retrofit2.CompletableFutureCallAdapterFactory
* retrofit2.DefaultCallAdapterFactory
at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:272)
at retrofit2.Retrofit.callAdapter(Retrofit.java:237)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:114)
... 7 more
我的 gradle 依赖项类似于:-
dependencies {
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '5.0.0-alpha.2'
// https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor
implementation group: 'com.squareup.okhttp3', name: 'logging-interceptor', version: '5.0.0-alpha.2'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.9.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-scalars
implementation group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.9.0'
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
}
我犯了什么基本错误?
解决方案
推荐阅读
- ruby-on-rails - 测试评论控制器。我无法通过创建评论的测试
- python - 如何在 Python 中使用 XMLRPC 在 Odoo 中创建延期交货?
- python - 为什么我的 argparse 程序不继续执行下一行?
- java - 如何在 Android Datepicker 中更改星期几的缩写?
- arrays - 将字符串数组设置为组合框列表会在 Visual Basic 中给出 Argument is required 错误
- c# - 来自 TextFile 的实时数据
- oracle - Oracle - LAST_DAY 和 TRUNC - 执行顺序
- elasticsearch - 更新索引不起作用 - Elastic Search 6.2.2
- angular - 使用输入 ngFor 访问表中的值更改
- javascript - 获取所有质数到 n 的函数