Tag: 匕首 2

使用flatMap的Rx Java Retrofit只运行一次

我试图从一个按钮点击流,然后平面地图与另一个可观察的改造,以请求一个端点,但它只能运行一次观察! 当我再次点击时,不要通过平面地图方法。 这是奇怪的原因,当平面地图返回其他observable它运行正常,但与改造之一,它只能运行一次。 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var retrofit:Retrofit = Retrofit.Builder() .baseUrl("<SERVER_IP>") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .build() var testService:TestService = retrofit.create(TestService::class.java) var buttonObservable:Observable<Void> = RxView.clicks(btnRequest) buttonObservable .observeOn(Schedulers.newThread()) .subscribeOn(AndroidSchedulers.mainThread()) .flatMap { Log.i("debug", "run flatmap") var request:Request = Request() request.accessToken = "<acess_token>" testService.test(request) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ user -> Log.i("debug", "user ") }, { err -> […]

从Activity 中调用Fragments演示者的方法

我有2个不同的片段,他们都将通过2个不同的活动使用。 在两个活动的布局中,都有一个框架布局用作碎片容器。 哪个是; 碎片:MapFragment,ListFragment 活动:HomeActivity,SearchActivity。 让我们谈谈HomeActivity; HomeActivity会将ListFragment和MapFragment注入到onCreate活动中,并将注入的片段推送到它的布局(首先是列表片段),之后活动将调用注入片段的呈现器方法在列表或地图上显示服务结果。 代码; 片段一面; @CustomScope @Component(dependencies = NetComponent.class, modules = {ChallengeListFrgModule.class, ChallengeRepositoryModule.class}) public interface ChallengeListFrgComponent{ void inject(ChallengeListFragment fragment); } _ @Module class ChallengeListFrgModule(private val mView: ChallengeListFrgContract.View) { @Provides internal fun providesChallengeListFrgContractView(): ChallengeListFrgContract.View { return mView } } ListFrgPresenter: public class ChallengeListFrgPresenter implements ChallengeListFrgContract.Presenter { //region Variables ChallengeListFrgContract.View mView; ChallengeRepository challengeRepository; […]

Android Retrofit 2同步通话

我需要同步调用刷新身份验证令牌,但我得到了“ NetworkOnMainThreadException ”。 @Synchronized fun refreshToken(): String? { val refreshToken = AuthService.getRefreshToken(context) var id_token: String? = null val call = apiService.refreshToken("refresh_token", refreshToken) val response = call.execute() id_token = response.body() if (id_token != null) { AuthService.saveTokens(context, id_token) } }) return id_token } 我应该如何避免“NetworkOnMainThreadException”? D/OkHttp: –> POST https://localhost/api/auth/refresh http/1.1 (372-byte body) D/OkHttp: <– HTTP FAILED: android.os.NetworkOnMainThreadException D/AndroidRuntime: […]

匕首注入超类和子类

BaseFragment类 open class BaseFragment : Fragment() { @Inject lateinit var apiManager: ApiManager @Inject lateinit var eventBus: EventBus override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) App.getInstance().component.inject(this) } override fun onStop() { eventBus.unreg(this) super.onStop() } override fun onStart() { super.onStart() eventBus.reg(this) } } RoomsFragment类 class RoomsFragment : BaseFragment() { @Inject lateinit var roomAdapter: RoomsAdapter override fun onCreate(savedInstanceState: Bundle?) […]

匕首2,不能在我的活动中注入演示者

我是Dagger2的新手。 我有一个注射主持人在我的活动中的问题我尝试解决我的问题后阅读本文https://android.jlelse.eu/inject-interfaces-without-providing-in-dagger-2-618cce9b1e29,但它不是帮助我。 我希望有人帮助我,我花了整天的时间来解决它… 这里是我的模块: @Module class AppModule(private val appContext: Context) { @Singleton @Provides internal fun provideContext(): Context { return appContext } @Singleton @Provides internal fun providePreferences(): SharedPreferences { return appContext.getSharedPreferences( appContext.resources.getString(R.string.shared_preferences_name), Context.MODE_PRIVATE) } } @Module abstract class ActivityModule { @Binds abstract fun provideMakeCheckPresenter (p : MakeCheckPresenter<MakeCheckMvpView>) : MakeCheckMvpPresenter<MakeCheckMvpView> } 这里是我的组件: @Component(modules = { AppModule.class, ActivityModule.class}) […]

格式@Query参数 – 改造2

我切换到使用Retrofit2,一切都很好,干净…但有一个服务调用,要求在正则表达式内查询参数(不要问我为什么,我已经要求它被改变)。 以下是我的方法: @GET("prod/search") fun searchProducts(@Query("q") q: String?): Call<Array<Product>> 如果我调用searchProducts("lala") ,查询将如下所示: prod/search?q=lala ,但是我需要它看起来像prod/search?q=(?i)\\Qlala\\E 有一种简单的方法来格式化查询参数来做到这一点?

在Dagger 2中提供方法

我有问题,我不知道为什么,但我的变量不注入和等于null。 我提供这样的方法: @Provides @Singleton fun provideSource(context: Context): DatabaseSource = DatabaseSource(context, Models.DEFAULT, DB_NAME, DB_VERSION) @Provides @Singleton fun provideStoreDB(databaseSource: DatabaseSource): KotlinReactiveEntityStore<Store> = KotlinReactiveEntityStore(KotlinEntityDataStore(databaseSource.configuration)) @Provides @Singleton @NotNull fun provideStores() : Database<Store> = PersistentDatabase(Store::class) 和模特商店的类: 它是“持久”阶级的继承人。 @Entity data class Store(val id: Long = 0, val name: String = "", val description: String = "", val date: String = "") […]

无法转换为org.jetbrains.kotlin.java.model.types.JeClassInitializerExecutableTypeMirror

我想升级到匕首2.8 – 但是现在我的项目在编译时会抛出这个错误: :android:compileWithAnalyticsWithCloudProdDebugAndroidTestKotlinAn exception occurred during annotation processing. Stacktrace: java.lang.ClassCastException: org.jetbrains.kotlin.java.model.types.JeMethodExecutableTypeMirror cannot be cast to org.jetbrains.kotlin.java.model.types.JeClassInitializerExecutableTypeMirror at org.jetbrains.kotlin.annotation.processing.impl.KotlinTypes.isSubsignature(KotlinTypes.kt:275) at dagger.shaded.auto.common.Overrides$ExplicitOverrides.isSubsignature(Overrides.java:183) at dagger.shaded.auto.common.Overrides$ExplicitOverrides.overrides(Overrides.java:109) at dagger.shaded.auto.common.MoreElements.getLocalAndInheritedMethods(MoreElements.java:334) at dagger.shaded.auto.common.MoreElements.getLocalAndInheritedMethods(MoreElements.java:314) at dagger.internal.codegen.ComponentValidator.validate(ComponentValidator.java:165) at dagger.internal.codegen.ComponentProcessingStep.process(ComponentProcessingStep.java:123) at dagger.internal.codegen.ComponentProcessingStep.process(ComponentProcessingStep.java:47) at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:329) at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:182) at org.jetbrains.kotlin.annotation.AbstractAnnotationProcessingExtension.doRound(AnnotationProcessingExtension.kt:346) at org.jetbrains.kotlin.annotation.AbstractAnnotationProcessingExtension.process(AnnotationProcessingExtension.kt:283) at org.jetbrains.kotlin.annotation.AbstractAnnotationProcessingExtension.doAnnotationProcessing(AnnotationProcessingExtension.kt:269) at org.jetbrains.kotlin.annotation.AbstractAnnotationProcessingExtension.analysisCompleted(AnnotationProcessingExtension.kt:140) at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:119) at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationWithCustomContext(TopDownAnalyzerFacadeForJVM.kt:66) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.analyze(KotlinToJVMBytecodeCompiler.kt:365) at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:126) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:358) […]

将自定义类的响应主体改造2

目前,我使用retrofit2来调用宁静的apis并得到回应。 因为响应体可以是多个类型,所以我写了下面的代码。 //Interface @FormUrlEncoded @POST("payments/events/{id}") fun postPayment(@Path("id") id: String): Call<Any> //Api Manager fun postPayment(id: String): Observable<Any> { return Observable.create { subscriber -> val callResponse = api.postPayment(id) val response = callResponse.execute() if (response.isSuccessful) { if (response.body() is MyClass1) { // never success… } else if (response.body() is MyClass2) { // never success… } subscriber.onNext(response.body()) subscriber.onCompleted() } […]

改造:在改装后得到一个空的机构

回应改进后,我得到一个空的机构。 这是我从我的日志中得到的: V/test: Response{protocol=http/1.1, code=200, message=, url= 这是我的代码: val call: Call<ResponseBody> = service.getTestCase() call.enqueue(object:Callback<ResponseBody> { override fun onResponse(call:Call<ResponseBody>, response:Response<ResponseBody>) { if (response.isSuccessful()) { // tasks available Log.v("Test",response.body().toString()) Log.v("test", response.toString()) } else { // error response, no access to resource? } } override fun onFailure(call:Call<ResponseBody>, t:Throwable) { // something went completely south (like no internet connection) […]

Kotlin language will be the best programming language for Android.