Skip to content
Retrofit RxAndroid 带生命周期的适配器,在Activity或Fragment调用网络请求时不用担心什么时候释放问题。
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
gradle/wrapper
library
.gitignore
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

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'
}
You can’t perform that action at this time.