我正在尝试使用RxJava进行Retrofit调用。当我在SubscribeOn上编写此代码时,它说“无法解析方法SubscribeOn(io.reactivex.scheduler)”。
您能指导我做错了什么地方吗?
有帮助吗?这是我的代码 当我在SubscribeOn上编写此代码时,它说“无法解析方法SubscribeOn(io.reactivex.scheduler)”。
java
public class MainActivity extends AppCompatActivity {
EditText etEmail,etPassword;
Button btnLogin;
String loginAdmin;
// RestCall call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CALL_NETWORK();
}
});
}
public void CALL_NETWORK() {
final RestCall call = RestClient.createService(RestCall.class);
call.LOGIN_RESPONSE_CALL("loginAdmin",etEmail.getText().toString(),etPassword.getText().toString())
//subscribeOn has the error.
.subscribeOn(Schedulers.io())//Cannot resolve method 'subscribeOn(rx.Scheduler)'
.observeOn(Schedulers.newThread())
.subscribe(new Subscriber<LoginResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(LoginResponse loginResponse) {
}
});
}
}
RestCall
interface RestCall {
@FormUrlEncoded
@POST("login.php")
Call<LoginResponse> LOGIN_RESPONSE_CALL(
@Field("loginAdmin") String loginAdmin,
@Field("email") String email,
@Field("password") String password
);
}
RestClient
public class RestClient {
public static <S> S createService(Class<S> serviceClass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(190, TimeUnit.SECONDS)
.writeTimeout(200, TimeUnit.SECONDS)
.readTimeout(190, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.addInterceptor(interceptor).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(APIConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
Retrofit retrofit = builder.build();
return retrofit.create(serviceClass);
}
依赖项
implementation 'com.squareup.okhttp:okhttp:2.5.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.google.code.gson:gson:2.8.4'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.1.8'