/retrofit-rxandroid-adapter

[已废弃] 请使用RxJava的compose特性实现。 Retrofit RxAndroid 带生命周期的适配器,在Activity或Fragment调用网络请求时不用担心什么时候释放问题。

Primary LanguageJava

RxAndroid Adapter

An Adapter for adapting RxJava 2.x types.

Usege

Add RxAndroidCallAdapterFactory as a Call adapter when building your Retrofit instance:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addCallAdapterFactory(RxAndroidCallAdapterFactory.create())
    .build();

Your service methods can now use any of the above types as their return type.

interface MyService {
  @GET("/user")
  AndroidObservable<User> getUser();
}

Note: method .subscribeOn(AndroidSchedulers.mainThread()) is call in AndroidObservable.

Call In Activity:

public class MyActivity extends Activity {

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		MyService service = retrofit.create(MyService.class);
		service.getUser().with(this).subscribe(new YourObserver());
	}
}

Call In Fragment

public class MyFragment extends Fragment {

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyService service = retrofit.create(MyService.class);
		service.getUser().with(this).subscribe(new YourObserver());
	}
}

Download

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
dependencies {
	implementation 'com.github.raedev:retrofit-rxandroid-adapter:latest.version'
}