I wanted to integrate retrofit
with RxJava
, but it cannot resolve symbol 'RxJavaCallAdapterFactory'.
import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface RetrofitService {
@FormUrlEncoded
@POST("api/do.php")
Observable<String> getUserToken(@Field("action") String action,
@Field("name") String name,
@Field("password") String password);
}
import android.leedev.cn.readertool.R;
import android.leedev.cn.readertool.model.http.service.RetrofitService;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login();
}
private void login() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.xingjk.cn/")
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RetrofitService service = retrofit.create(RetrofitService.class);
Observable<String> observable = service.getUserToken("loginIn", "******", "6205240");
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.i(TAG, "onSubscribe: ");
}
@Override
public void onNext(String s) {
Log.i(TAG, "onNext: " + s.toString());
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: ");
}
});
}
}
Looking forward to your help, thank you!
Add:
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
to your dependencies
.
See the documentation.
Also update from RxJavaCallAdapterFactory to RxJava2CallAdapterFactory
you need to add this:
compile group: 'com.squareup.retrofit2', name: 'adapter-rxjava2', version: '2.3.0'
in gradle dependencies
and take a look on this : https://stackoverflow.com/a/37733206/3637755
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With