首页 > 解决方案 > 是什么导致了这个 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'
}

我犯了什么基本错误?

标签: retrofit2

解决方案


推荐阅读