管理retrofit调用的生命周期 – RetrofitLifecycle

RetrofitLifecycle

RetrofitLifecycle manage retrofit call’s lifecycle with proxy class which generated by annotation.

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency

dependencies {
    compile 'com.github.luckyandyzhang.RetrofitLifecycle:retrofit-lifecycle:1.0.0'
    annotationProcessor 'com.github.luckyandyzhang.RetrofitLifecycle:retrofit-lifecycle-compiler:1.0.0'    
}

Use@RetrofitInterfaceon retrofit interface:

@RetrofitInterface
public interface GankAPI {
    @GET("data/Android/10/1")
    Call<ResponseBody> getGankList();
}

Get proxy class and use it:

GankAPI gankAPI = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("http://gank.io/api/")
        .build()
        .create(GankAPI.class);
//get proxy class
GankAPI gankAPIProxy = RetrofitLifecycle.getProxyInterface(GankAPI.class, gankAPI);
//use proxy class to reqeust
gankAPIProxy.getGankList().enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {

    }
});
//cancel all calls
RetrofitLifecycle.cancelAll(gankAPIProxy);

For additional information see sample
module 🙂

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:管理retrofit调用的生命周期 – RetrofitLifecycle