Java – how to make customization implement retrofit2 Call

I'm using retrofit2 and I want to overwrite its call Enqueue method

I have done so far:

Custom phone:

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

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

Bees:

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

But I keep getting these mistakes:

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

and

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

How can this be done correctly in any way? Thank you in advance!

Solution

First create a servicemanager class –

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 is a class containing service endpoints

final class ServiceApiEndpoints {

    public static final String SERVICE_ENDPOINT = "your_app_url";
}

Create interface apiservice

public interface APIService {
 String GET_INFO = "get_info";

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

Create a resinfo model

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(){

    }
}

Call request

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) {

    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>