我的应用程序需要连接到
使用
. 所以我创建了下一个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>
}
我知道我可以提供一个类来描述JSON结构和
格森
在我为
改装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()
}
}
并连接
匕首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
没有调用方法。
我做错了什么?它必须呼叫
至少没有任何相关的错误
LogCat
.
我已经签入调试器应用程序检索授权代码,它不是
null
.