随机处理 retrofit2 请求
Random processing of retrofit2 requests
我得到了一份指数成分列表(102 个代码),我想了解有关它们的详细信息,但是在 102 个查询中,总是执行不超过 10 个,并且代码是随机选择的。所有请求都使用 RxJava3 通过 retrofit2 执行。可能是什么问题呢?
这是 ViewModel 代码:
var price: MutableLiveData<CompanyInfoModel> = MutableLiveData()
fun getCompanyInfoObserver(): MutableLiveData<CompanyInfoModel> {
return price
}
fun makeApiCall(ticker: String) {
val retrofitInstance = RetrofitYahooFinanceInstance.getRetrofitInstance().create(RetrofitService::class.java)
retrofitInstance.getCompanyInfo(ticker)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getCompanyInfoObserverRx())
}
private fun getCompanyInfoObserverRx(): Observer<CompanyInfoModel> {
return object : Observer<CompanyInfoModel> {
override fun onComplete() {
// Hide progress bar
}
override fun onError(e: Throwable?) {
price.postValue(null)
}
override fun onNext(t: CompanyInfoModel?) {
price.postValue(t)
}
override fun onSubscribe(d: Disposable?) {
// Show progress bar
}
}
}
下面是模型的初始化:
companyInfoModel = ViewModelProvider(this).get(CompanyInfoViewModel::class.java)
companyInfoModel.getCompanyInfoObserver().observe(this, Observer<CompanyInfoModel> { it ->
if(it != null) {
retrieveList(Helper.companyInfoToStock(it))
}
else {
Log.e(TAG, "Error in fetching data")
}
})
这里是请求方法本身:
fun getCompanyInfo(ticker: String) {
companyInfoModel.makeApiCall(ticker)
}
谢谢你,帕维尔。问题确实出在 API 限制中,我更改了提供程序,一切都开始正常工作。
我得到了一份指数成分列表(102 个代码),我想了解有关它们的详细信息,但是在 102 个查询中,总是执行不超过 10 个,并且代码是随机选择的。所有请求都使用 RxJava3 通过 retrofit2 执行。可能是什么问题呢? 这是 ViewModel 代码:
var price: MutableLiveData<CompanyInfoModel> = MutableLiveData()
fun getCompanyInfoObserver(): MutableLiveData<CompanyInfoModel> {
return price
}
fun makeApiCall(ticker: String) {
val retrofitInstance = RetrofitYahooFinanceInstance.getRetrofitInstance().create(RetrofitService::class.java)
retrofitInstance.getCompanyInfo(ticker)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getCompanyInfoObserverRx())
}
private fun getCompanyInfoObserverRx(): Observer<CompanyInfoModel> {
return object : Observer<CompanyInfoModel> {
override fun onComplete() {
// Hide progress bar
}
override fun onError(e: Throwable?) {
price.postValue(null)
}
override fun onNext(t: CompanyInfoModel?) {
price.postValue(t)
}
override fun onSubscribe(d: Disposable?) {
// Show progress bar
}
}
}
下面是模型的初始化:
companyInfoModel = ViewModelProvider(this).get(CompanyInfoViewModel::class.java)
companyInfoModel.getCompanyInfoObserver().observe(this, Observer<CompanyInfoModel> { it ->
if(it != null) {
retrieveList(Helper.companyInfoToStock(it))
}
else {
Log.e(TAG, "Error in fetching data")
}
})
这里是请求方法本身:
fun getCompanyInfo(ticker: String) {
companyInfoModel.makeApiCall(ticker)
}
谢谢你,帕维尔。问题确实出在 API 限制中,我更改了提供程序,一切都开始正常工作。