Retrofit 2 不执行请求
Retrofit 2 doesn't perform a request
我的应用需要使用 OAuth 2.0 连接到 GitHub.com。所以我创建了下一个 API 模型:
interface IGitHubApi {
@GET("login/oauth/access_token")
fun getAccessToken(
@Query("client_id") clientId: String = GitHub.CLIENT_ID,
@Query("client_secret") clientSecret: String = GitHub.CLIENT_SECRET,
@Query("code") code: String
): Single<String>
}
我知道我可以提供一些 class 来描述 JSON 结构,Gson 会自动反序列化它,但我想看到一个简单的 JSON (作为字符串)。
在我为 Retrofit 2 创建组件和模块后:
@Component(modules = [GitHubApiModule::class])
interface IGitHubApiComponent {
fun getGitHubService(): IGitHubApi
}
@Module
class GitHubApiModule {
@Provides
fun provideGitHubApi(retrofit: Retrofit): IGitHubApi {
return retrofit.create(IGitHubApi::class.java)
}
@Provides
fun provideRetrofit(gsonConverterFactory: GsonConverterFactory,
rxJava2CallAdapterFactory: RxJava2CallAdapterFactory): Retrofit {
return Retrofit.Builder()
.baseUrl(GitHub.BASE_URL)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build()
}
@Provides
fun provideGson(): Gson {
val gsonBuilder = GsonBuilder()
return gsonBuilder.create()
}
@Provides
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory {
return GsonConverterFactory.create(gson)
}
@Provides
fun provideRxJava2CallAdapterFactory(): RxJava2CallAdapterFactory {
return RxJava2CallAdapterFactory.create()
}
}
并将 Dagger 2 连接到我的视图模型:
class AccountViewModel(app: Application) : AndroidViewModel(app) {
var label: ObservableField<String> = ObservableField()
var isLoading: ObservableBoolean = ObservableBoolean(true)
var progress: ObservableInt = ObservableInt(0)
private var gitHubApi: IGitHubApi = DaggerIGitHubApiComponent.create().getGitHubService()
private fun getErrorMsg(app: Application, hasAccount: Boolean): String {
return if (hasAccount) {
app.getString(R.string.err_account_update_failed)
} else {
app.getString(R.string.err_account_creation_failed)
}
}
fun createOrUpdateAccount(authorizationCode: String?) {
val app = getApplication<App>()
val accountType = app.getString(R.string.account_type_git)
val accountManager = AccountManager.get(app.applicationContext)
val accounts = accountManager.getAccountsByType(accountType)
label.set(app.getString(R.string.msg_account_creating))
if (authorizationCode == null) {
val msg = getErrorMsg(app, accounts.isNotEmpty())
label.set(msg)
return //Break operation
}
gitHubApi.getAccessToken(code = authorizationCode)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
label.set(it)
progress.set(100)
isLoading.set(false)
},
onError = {
Log.e("AccountViewModel", it.message?: "Could not retrieve an access token.", it)
val msg = getErrorMsg(app, accounts.isNotEmpty())
label.set(msg)
}
)
.dispose()
}
}
当我 运行 我的应用程序时,我只看到 Creating an account...
标签,这意味着 onSuccess
和 onError
方法没有被调用。
我做错了什么?它必须至少调用 onError
并且 LogCat
.
中没有任何相关错误
我在调试器应用程序中检查过检索授权码,它不是 null
。
主要原因:dispose()
本质上是取消,处理后不会收到成功或错误事件。
AndroidStudio asks to call this method
subscribeBy()
用 @CheckReturnValue
注释,Studio 抱怨您忽略了返回的 Disposable
.
对于 fire-and-forget 请求,您可以忽略一次性请求并抑制 IDE 警告。
如果您想限定请求的范围并能够取消它,例如当导航到另一个屏幕时,您可以将 Disposable
存储到一个变量中,并在适当的生命周期方法中对其调用 dispose()
。如果您有多个此类请求,可以使用 CompositeDisposable
将它们捆绑在一起。
在你的评论中你说你有 ViewModel
。在 ViewModel
中,您可以覆盖 onCleared()
并在那里处理调用。
我的应用需要使用 OAuth 2.0 连接到 GitHub.com。所以我创建了下一个 API 模型:
interface IGitHubApi {
@GET("login/oauth/access_token")
fun getAccessToken(
@Query("client_id") clientId: String = GitHub.CLIENT_ID,
@Query("client_secret") clientSecret: String = GitHub.CLIENT_SECRET,
@Query("code") code: String
): Single<String>
}
我知道我可以提供一些 class 来描述 JSON 结构,Gson 会自动反序列化它,但我想看到一个简单的 JSON (作为字符串)。
在我为 Retrofit 2 创建组件和模块后:
@Component(modules = [GitHubApiModule::class])
interface IGitHubApiComponent {
fun getGitHubService(): IGitHubApi
}
@Module
class GitHubApiModule {
@Provides
fun provideGitHubApi(retrofit: Retrofit): IGitHubApi {
return retrofit.create(IGitHubApi::class.java)
}
@Provides
fun provideRetrofit(gsonConverterFactory: GsonConverterFactory,
rxJava2CallAdapterFactory: RxJava2CallAdapterFactory): Retrofit {
return Retrofit.Builder()
.baseUrl(GitHub.BASE_URL)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build()
}
@Provides
fun provideGson(): Gson {
val gsonBuilder = GsonBuilder()
return gsonBuilder.create()
}
@Provides
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory {
return GsonConverterFactory.create(gson)
}
@Provides
fun provideRxJava2CallAdapterFactory(): RxJava2CallAdapterFactory {
return RxJava2CallAdapterFactory.create()
}
}
并将 Dagger 2 连接到我的视图模型:
class AccountViewModel(app: Application) : AndroidViewModel(app) {
var label: ObservableField<String> = ObservableField()
var isLoading: ObservableBoolean = ObservableBoolean(true)
var progress: ObservableInt = ObservableInt(0)
private var gitHubApi: IGitHubApi = DaggerIGitHubApiComponent.create().getGitHubService()
private fun getErrorMsg(app: Application, hasAccount: Boolean): String {
return if (hasAccount) {
app.getString(R.string.err_account_update_failed)
} else {
app.getString(R.string.err_account_creation_failed)
}
}
fun createOrUpdateAccount(authorizationCode: String?) {
val app = getApplication<App>()
val accountType = app.getString(R.string.account_type_git)
val accountManager = AccountManager.get(app.applicationContext)
val accounts = accountManager.getAccountsByType(accountType)
label.set(app.getString(R.string.msg_account_creating))
if (authorizationCode == null) {
val msg = getErrorMsg(app, accounts.isNotEmpty())
label.set(msg)
return //Break operation
}
gitHubApi.getAccessToken(code = authorizationCode)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
label.set(it)
progress.set(100)
isLoading.set(false)
},
onError = {
Log.e("AccountViewModel", it.message?: "Could not retrieve an access token.", it)
val msg = getErrorMsg(app, accounts.isNotEmpty())
label.set(msg)
}
)
.dispose()
}
}
当我 运行 我的应用程序时,我只看到 Creating an account...
标签,这意味着 onSuccess
和 onError
方法没有被调用。
我做错了什么?它必须至少调用 onError
并且 LogCat
.
我在调试器应用程序中检查过检索授权码,它不是 null
。
主要原因:dispose()
本质上是取消,处理后不会收到成功或错误事件。
AndroidStudio asks to call this method
subscribeBy()
用 @CheckReturnValue
注释,Studio 抱怨您忽略了返回的 Disposable
.
对于 fire-and-forget 请求,您可以忽略一次性请求并抑制 IDE 警告。
如果您想限定请求的范围并能够取消它,例如当导航到另一个屏幕时,您可以将 Disposable
存储到一个变量中,并在适当的生命周期方法中对其调用 dispose()
。如果您有多个此类请求,可以使用 CompositeDisposable
将它们捆绑在一起。
在你的评论中你说你有 ViewModel
。在 ViewModel
中,您可以覆盖 onCleared()
并在那里处理调用。