Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol 'RxJavaCallAdapterFactory'

I wanted to integrate retrofit with RxJava, but it cannot resolve symbol 'RxJavaCallAdapterFactory'.

my code

my dependencies

Here is my code

RetrofitService.java

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);
}

MainActivity.java

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!

like image 639
Thomas Lee Avatar asked Dec 10 '17 18:12

Thomas Lee


2 Answers

Add:

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

to your dependencies.

See the documentation.

Also update from RxJavaCallAdapterFactory to RxJava2CallAdapterFactory

like image 186
CommonsWare Avatar answered Oct 09 '22 00:10

CommonsWare


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

like image 39
Elyes Avatar answered Oct 09 '22 02:10

Elyes