通过改造返回网络呼叫上的Observable

问题描述 投票:1回答:1

我正在使用API​​返回的项目创建一个简单的回收站视图列表。我有我的改装客户

open class CoinListRetroFit {

val httpLoggingInterceptor = HttpLoggingInterceptor()
val okHttpClient = OkHttpClient.Builder()
        .readTimeout(1000, TimeUnit.SECONDS)
        .addInterceptor(httpLoggingInterceptor)
        .build()


private val retroFit: Retrofit = Retrofit.Builder()
        .baseUrl(" https://api.coinmarketcap.com/v2/")
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()

val coinList: LuckyCoinApiService = retroFit.create(LuckyCoinApiService::class.java)

我有一个Api服务和客户打电话

interface LuckyCoinApiService {

@GET("listings/")
fun getCoinListing() : Observable<CoinListResponse>

}

class LuckyCoinApiClient : CoinListRetroFit() {

fun getCoins(): Observable<List<CoinListItem>> =
        coinList.getCoinListing().map { response ->
            response.cryptoList
        }

}

现在下面是im订阅observable并填充我的列表,但是它返回null,当我去调试时,它在coinList.getCoinListing上的Client类中崩溃。我没有得到有关网络电话的信息,这是我认为OkHttpClient Interceptor的用途。这是我订阅可观察的地方......

  addDisposable(LuckyCoinApiClient()
            .getCoins()
            .compose(ObservableTransformer { upstream: Observable<List<CoinListItem>> ->
                upstream
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
            })
            .subscribe({ response ->
                list = response
                adapter.addItems(list!!)
            },
                    { _ ->
                        Log.e("CoinListController", "Error retrieving list!!")
                    }))
}



    fun addDisposable(disposable: Disposable) {
    if (compositeDisposable == null) {
        compositeDisposable = CompositeDisposable(disposable)
    } else {
        compositeDisposable!!.add(disposable)
    }
}

我一定是误解了如何使这些请求有效。以及如何正确设置日志拦截器

编辑:

添加Internet权限并对我的observable进行一些更改并在我的拦截器上执行.setLevel()之后。我现在可以在LogCat中看到网络调用了,我得到了预期的200 OK响应列表..

但是现在响应停止了

    08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:             "id": 3238, 
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:             "name": "ABCC Token", 
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:             "symbol": "AT", 
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:             "website_slug": "abcc-token"
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:         }, 
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:         {
08-30 15:06:25.446 5214-5238/com.example.luckycoins D/OkHttp:             "id": 3239, 

它停在那个id而没有阅读它的属性。还有2-3个未达到的清单项目。

android retrofit rx-java2 okhttp
1个回答
1
投票

你的adapter.addItems(list!!)在溪流区之外。你不应该通过副作用更新你的UI。由于您的api请求将位于io线程池的其他线程上,因此您的适配器很可能在api调用完成之前使用空列表更新自身。

你应该做的是更新subscribe({})块内的适配器。

此外,您不应强制打开可以为空的列表。你应该在kotlin中做的是用你在Java中用它做的空检查来包装它,或者像这样使用let运算符list?.let{ list ->}

© www.soinside.com 2019 - 2024. All rights reserved.