加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何使定制实现Retrofit2.Call

发布时间:2020-12-14 16:38:44 所属栏目:Java 来源:网络整理
导读:我正在使用Retrofit2,我想覆盖其Call.enqueue方法. 我到目前为止这样做: 自定义电话: public class CustomCallT implements CallT { private final CallT delegate; //..every method has delegate method invoked in it 蜜蜂: @GET CustomCallTKBaseResp
我正在使用Retrofit2,我想覆盖其Call.enqueue方法.

我到目前为止这样做:

自定义电话:

public class CustomCall<T> implements Call<T> {

        private final Call<T> delegate;
        //..every method has delegate method invoked in it

蜜蜂:

@GET
        CustomCall<TKBaseResponse> testConnection(@Url String customUrl);

但是我不断得到这些错误:

Unable to create call adapter for CustomCall<....>

Could not locate call adapter for CustomCall<....>

任何方式如何正确地做到这一点?提前致谢!

解决方法

首先创建一个ServiceManager类 –
public final class ServiceManager {

    private static ServiceManager sServiceManager;

    /**
     * Gets the instance of the web services implementation.
     *
     * @return the singleton instance.
     */
    public static ServiceManager get() {
        if (sServiceManager == null) {
            sServiceManager = new ServiceManager();
        }
        return sServiceManager;
    }

    /**
     * Creates the services for a given HTTP Url,useful when testing
     * through multiple endpoints and unit testing
     *
     * @param clazz the service class.
     * @param <T>   type of the service.
     * @return the created services implementation.
     */
    public <T> T createService(Class<T> clazz) {
        return createService(clazz,HttpUrl.parse(ServiceApiEndpoints.SERVICE_ENDPOINT));
    }

    /**
     * Creates the services for a given HTTP Url,useful when testing
     * through multiple endpoints and unit testing
     *
     * @param clazz   the service class.
     * @param httpUrl the endpoint
     * @param <T>     type of the service.
     * @return the created services implementation.
     */
    public <T> T createService(Class<T> clazz,HttpUrl httpUrl) {
        Retrofit retrofit = getRetrofit(httpUrl);
        return retrofit.create(clazz);
    }

    public <T> T createService(Class<T> clazz,Retrofit retrofit) {
        return retrofit.create(clazz);
    }

    private Retrofit getRetrofit(HttpUrl httpUrl) {
        return new Retrofit.Builder()
                .baseUrl(httpUrl)
                .client(createClient())
                .addConverterFactory(getConverter())
                .build();
    }

    public Retrofit getPlainRetrofit(HttpUrl httpUrl) {
        return new Retrofit.Builder()
                .baseUrl(httpUrl)
                .client(new OkHttpClient.Builder().build())
                .addConverterFactory(getConverter())
                .build();
    }

    private Converter.Factory getConverter() {
        return GsonConverterFactory.create();
    }


    private OkHttpClient createClient() {
        return new OkHttpClient.Builder().addInterceptor(new RequestInterceptor()).build();
    }

}

ServiceApiEndpoints是一个包含服务端点的类.

final class ServiceApiEndpoints {

    public static final String SERVICE_ENDPOINT = "your_app_url";
}

创建接口APIService

public interface APIService {
 String GET_INFO = "get_info";

    @GET(GET_INFO)
    Call<ResInfo[]> getInfo();
}

创建ResInfo模型.

public class ResInfo {
    private static final String FIELD_CONTENT = "content";

    public String getContent() {
        return mContent;
    }

    public void setContent(final String content) {
        mContent = content;
    }


    @SerializedName(FIELD_CONTENT)
    private String mContent;

    public ResInfo(){

    }
}

调用请求.

private Call<ResInfo[]> mGetInfoAPICall;

    APIService apiService=ServiceManager.get().createService(APIService.class);
    mGetInfoAPICall = apiService.getInfo();
    mGetInfoAPICall.enqueue(new Callback<ResInfo[]>() {
    @Override
    public void onResponse(Call<ResInfo[]> call,Response<ResInfo[]> response) {

    }

    @Override
    public void onFailure(Call<ResInfo[]> call,Throwable t) {

    }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读