我正在关注Github Link使用ReteroFit和RxJava来使用API,但是在我的接口类中将类型从Call更改为Observer时,我收到一条错误消息。
类型'java.util.Observable'没有类型参数
ApiClient:
public class ApiClient {
public static final String BASE_URL = "*********";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
if (retrofit==null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
ApiInterface
public interface ApiInterface {
@FormUrlEncoded
@POST("user/get_user")
Observable<UserResponse> getUsers(@Field("email") String email, @Field("password") String password);
}
UserResponse:
public class UserResponse {
private List<User> result;
private String status_message;
private int status_code;
public int getStatus_code() {
return status_code;
}
public void setStatus_code(int status_code) {
this.status_code = status_code;
}
public List<User> getResult() {
return result;
}
public void setResult(List<User> result) {
this.result = result;
}
public String getStatus_message() {
return status_message;
}
public void setStatus_message(String status_message) {
this.status_message = status_message;
}
}
依赖关系:
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
答案 0 :(得分:5)
将代码中的import java.util.Observable;
更改为import io.reactivex.Observable;
答案 1 :(得分:1)
就像先前的答案所说的那样,错误,导入以解决此问题,删除导入,然后alt + enter
导入新的导入。
记录拦截器以备将来记录。
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
答案 2 :(得分:0)
您使用的是错误的Observable类:
输入'j ava.util .Observable'
而不是使用rx java Obserable