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);
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://api.fanyi.baidu.com/api/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .build(); GetRequest_Interface request = retrofit.create(GetRequest_Interface.class);
Observable.interval(3,5, TimeUnit.SECONDS) .doOnNext(aLong -> { Log.e(TAG, "第 " + aLong + " 次轮询" ); Observable<Translation> observable = request.getCall(); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Translation>() { @Override public void onSubscribe(@NonNull Disposable d) {
}
@Override public void onNext(@NonNull Translation translation) { translation.show(); }
@Override public void onError(@NonNull Throwable e) { Log.e(TAG, "onError: " ); }
@Override public void onComplete() {
} }); }).subscribe(new Observer<Long>() { @Override public void onSubscribe(@NonNull Disposable d) {
}
@Override public void onNext(@NonNull Long aLong) {
}
@Override public void onError(@NonNull Throwable e) { e.printStackTrace(); Log.e(TAG, "对Error事件作出响应"); }
@Override public void onComplete() { Log.e(TAG, "对Complete事件作出响应"); } }); } }
|