在下面的代码中,在伴随对象中,创建方法sould返回Single。如代码所示,我使用的最终运算符是flatmap(),用于将改造对象转换为Maybe.just,以确保其不为null。 但是,当我使用此运算符时
.flatMap {retrofit->Maybe.just(retrofit)}
我无法链接任何其他运算符,以将返回的值从上述平面图转换为Single。请让我知道为什么我不能在上述代码上链接任何其他运算符,
请找到下面发布的代码
代码
interface WikiApiService {
@GET("api.php")
fun hitCountCheck(
@Query("action") action: String,
@Query("format") format: String,
@Query("list") list: String,
@Query("srsearch") srsearch: String
):
Observable<ResponseObjectModelForQuery.ResultOfQuery>
@GET("api.php")
fun hitContinue2(
@Query("action") action: String,
@Query("format") format: String,
@Query("list") list: String,
@Query("srsearch") srsearch: String
):
Observable<ResponseObjectModelForContinueSrOffset.ResultOfContinueSrOffset>
@GET("api.php")
fun hitContinue3(
@Query("action") action: String,
@Query("format") format: String,
@Query("list") list: String,
@Query("srsearch") srsearch: String
):
Observable<ResponseObjectModelForContinueContinue.Continue>
//why do we need a companion object
companion object {
fun create(): Single<WikiApiService>? {
return Single.just(
Retrofit.Builder()
.addCallAdapterFactory(
RxJava2CallAdapterFactory.create()
)
.addConverterFactory(
GsonConverterFactory.create()
)
.baseUrl("https://en.wikipedia.org/w/")
.build())
//workin.flatMap { Maybe.just(it.create(WikiApiService::class.java)).toSingle()}
.flatMap {retrofit->Maybe.just(retrofit)}
.//cant chain any further operators
}
}
}
答案 0 :(得分:0)
您是否尝试过使用flatMapMaybe()
:
companion object {
fun create(): Single<WikiApiService>? {
return Single.just(
Retrofit.Builder()
.addCallAdapterFactory(
RxJava2CallAdapterFactory.create()
)
.addConverterFactory(
GsonConverterFactory.create()
)
.baseUrl("https://en.wikipedia.org/w/")
.build())
//workin.flatMap { Maybe.just(it.create(WikiApiService::class.java)).toSingle()}
.flatMapMaybe {retrofit->Maybe.just(retrofit)}
.//cant chain any further operators
}
}
}
?或根据您希望链接的内容将Maybe
转换为Single
:
companion object {
fun create(): Single<WikiApiService>? {
return Single.just(
Retrofit.Builder()
.addCallAdapterFactory(
RxJava2CallAdapterFactory.create()
)
.addConverterFactory(
GsonConverterFactory.create()
)
.baseUrl("https://en.wikipedia.org/w/")
.build())
//workin.flatMap { Maybe.just(it.create(WikiApiService::class.java)).toSingle()}
.flatMap {retrofit->Maybe.just(retrofit).toSingle() }
.//cant chain any further operators
}
}
}