使用RXjava + Retrofit,Plz帮助获得一些问题
Json喜欢:
https://api.androidhive.info/json/movies.json
以下错误屏幕排序:
下面的代码:
CallInterface movieApiInterface;
Call call;
public final static String BASEURL="https://api.androidhive.info/json/";
private OAuthManager(){}
public static OAuthManager getInstance(){
if(instance==null){
instance=new OAuthManager();
instance.getClient();
}
return instance;
}
private void getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
movieApiInterface = retrofit.create(CallInterface.class);
}
}
public interface CallInterface {
@GET("movies.json")
Observable<List<Movies>> getMoviesList();
}
public Observable<List<Movies>> getMovieApiData(){
return movieApiInterface.getMoviesList();
}
在简单的翻新呼叫中工作正常,但在Observable上面临此问题。
答案 0 :(得分:1)
从代码中我看不到问题,为什么不尝试使用rxjava2的任何RxJava2CallAdapterFactory?我在想当它得到json的值并尝试存储该值时,它被卡在.addCallAdapterFactory(RxJavaCallAdapterFactory.create())中,这就是它返回错误的原因
我可以举一些使用Retrofit的实现示例,以便您可以将其用作参考
在我的礼物中:
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "com.jakewharton.rxbinding2:rxbinding:2.1.1"
implementation "com.github.akarnokd:rxjava2-extensions:0.18.9"
implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
implementation "com.squareup.okhttp3:okhttp:3.10.0"
implementation "com.squareup.okhttp3:okhttp-urlconnection:3.10.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.4.1"
这是我的ApiClient:
public class ApiClient {
private static Retrofit retrofit = null;
private static OkHttpClient okHttpClient;
public static Retrofit getClient() {
if (okHttpClient == null)
initOkHttp();
if (retrofit == null) {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(
Travel.class,
new JsonTravelSerializer())
.create();
retrofit = new Retrofit.Builder()
.baseUrl(Const.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return retrofit;
}
private static void initOkHttp() {
int REQUEST_TIMEOUT = 60;
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Request-Type", "Android")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
});
okHttpClient = httpClient.build();
}}
您需要致电的课程
apiservice = ApiClient.getClient().create(ApiService.class);
.....
disposable.add(apiService
.getJson()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<JsonObject>() {
@Override
public void onSuccess(JsonObject jsonObject) {
Gson gson = new Gson();
String jsonOutput = jsonObject.get("data").toString();
Type listType = new TypeToken<List<TravelData>>() {
}.getType();
List<TravelData> travelData = gson.fromJson(jsonOutput, listType);
travelList.clear();
travelList.addAll(travelData);
adapter = new MainViewAdapter(getSupportFragmentManager());
adapter.addAll(travelList);
viewPager.setAdapter(adapter);
}
@Override
public void onError(Throwable e) {
}
})
);
答案 1 :(得分:1)
根据项目中已导入的Retrofit版本2.4.0
,您添加了Retrofit2RxJava2Adapter的已弃用版本:
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
Retrofit 2.2和更高版本具有用于RxJava 2的第一方呼叫适配器: https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2
您只需替换它,然后像这样使用:
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
和
retrofit = new Retrofit.Builder()
.baseUrl(Const.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // replace this line
.build();
答案 2 :(得分:0)
您能否按如下所示更改端点,然后重试。
public interface CallInterface {
@GET("movies.json")
Observable<Movies> getMoviesList();
}
public Observable<Movies> getMovieApiData(){
return movieApiInterface.getMoviesList();
}
请让我知道结果。