Retrofit + RxJava 没有发出 API 请求
Retrofit + RxJava Not making API Request
我对 Kotlin 中的 Retrofit 和 RxJava2 有疑问。
这是我的 build.gradle
:
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1' implementation 'com.squareup.retrofit2:converter-moshi:2.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.11'
我有以下带有单个请求的 Retrofit 界面,login
。我还有一个名为 User
的简单 data class
。
data class User (
private val firstName: String
)
interface ApiService {
@POST("auth/login")
fun login(@Body body: String) : Observable<User>
}
当我尝试发出请求并订阅它时,没有发送任何请求。我检查了我的服务器日志,服务器根本没有收到任何东西。应用程序日志中也没有错误。我不确定我做错了什么,因为我看了很多 articles/tutorials,他们都说要这样做。
val client = Retrofit.Builder()
.baseUrl("https://example.com/api/")
.addCallAdapterFactory(RxJava2CallAdapaterFactory.create())
.build()
.create(ApiService::class.java)
client.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
谁能给我解释一下我到底做错了什么?
编辑:
我已经尝试了下面的代码,但我仍然得到相同的结果。没有请求。
val okHttpClient = OkHttpClient.Builder().addInterceptor(
HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT)
).build()
val client = Retrofit.Builder()
.baseUrl("https://example.com/api/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
.create(ApiClient::class.java)
client.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ user -> println(user) }, { error -> println(error) })
您应该添加日志拦截器以查看日志。
val httpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
val builder = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(RxJava2CallAdapaterFactory.create())
val apiService = builder.client(httpClient).build().create(ApiService::class.java)
我假设您已将 INTERNET 权限添加到 AndroidManifest.xml
正如@sonnet 所建议的,您应该向您的请求添加回调
apiService.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ user -> ... }, { error -> ... })
您缺少向改造实例添加 Moshi 转换器。
.addConverterFactory(MoshiConverterFactory.create())
我对 Kotlin 中的 Retrofit 和 RxJava2 有疑问。
这是我的 build.gradle
:
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1' implementation 'com.squareup.retrofit2:converter-moshi:2.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.11'
我有以下带有单个请求的 Retrofit 界面,login
。我还有一个名为 User
的简单 data class
。
data class User (
private val firstName: String
)
interface ApiService {
@POST("auth/login")
fun login(@Body body: String) : Observable<User>
}
当我尝试发出请求并订阅它时,没有发送任何请求。我检查了我的服务器日志,服务器根本没有收到任何东西。应用程序日志中也没有错误。我不确定我做错了什么,因为我看了很多 articles/tutorials,他们都说要这样做。
val client = Retrofit.Builder()
.baseUrl("https://example.com/api/")
.addCallAdapterFactory(RxJava2CallAdapaterFactory.create())
.build()
.create(ApiService::class.java)
client.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
谁能给我解释一下我到底做错了什么?
编辑:
我已经尝试了下面的代码,但我仍然得到相同的结果。没有请求。
val okHttpClient = OkHttpClient.Builder().addInterceptor(
HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT)
).build()
val client = Retrofit.Builder()
.baseUrl("https://example.com/api/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
.create(ApiClient::class.java)
client.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ user -> println(user) }, { error -> println(error) })
您应该添加日志拦截器以查看日志。
val httpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
val builder = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(RxJava2CallAdapaterFactory.create())
val apiService = builder.client(httpClient).build().create(ApiService::class.java)
我假设您已将 INTERNET 权限添加到 AndroidManifest.xml
正如@sonnet 所建议的,您应该向您的请求添加回调
apiService.login(JSONObject().put("email", ...).toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ user -> ... }, { error -> ... })
您缺少向改造实例添加 Moshi 转换器。
.addConverterFactory(MoshiConverterFactory.create())