当我的用户登录成功时,我会请求同步我的本地数据库。 我通过改造和用户RxJava来执行此操作以压缩响应。
但是当我有一个SocketTimeoutException我的应用程序崩溃时,我不知道为什么。
这是我的代码:
private fun syncData(): Single<Boolean> {
getProgressTextView().text = getString(R.string.synchronizing)
val list: List<Single<Boolean>> = Arrays.asList(DocumentTypeManager.getAndSave(retrofit),
HomeAccessibilitiesManager.getAndSave(retrofit),
StreetTypesManager.getAndSave(retrofit),
IncidentTypesManager.getAndSave(retrofit),
TaskTypesManager.getAndSave(retrofit, this),
PatientFunituresManager.getAndSave(retrofit, this),
HealthMutualsManager.getAndSave(retrofit))
return Single.zip(list, {
it.forEach {
if (!(it as Boolean)) {
return@zip false
}
}
return@zip true
})
}
获取httpRequest
的对象示例object HomeAccessibilitiesManager : AnkoLogger {
fun getAndSave(retrofit: Retrofit): Single<Boolean> {
val t = retrofit.create(RestApi::class.java)
.getHomeAccessibilities()
return HttpRequestDataManager.getAndSave(t)
}
}
保存我的webService返回数据库的经理
object HttpRequestDataManager : AnkoLogger {
inline fun <reified T : io.realm.RealmModel> getAndSave(single: Single<List<T>>): Single<Boolean> {
return Single.create { subscriber ->
single
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ list ->
executeTransaction {
list.saveAll()
}
debug { "success sync, with ${list.size} results" }
subscriber.onSuccess(true)
},
{
if (it != null) {
warn { it.message }
subscriber.onError(it)
} else {
subscriber.onError(Throwable("unknow error"))
}
}
)
}
}
}
和RestApi
@GET("api/v1/home_accessibilities")
fun getHomeAccessibilities(): Single<List<RHomeAccessibilities>>
我也有一个JWTInterceptor
class JwtInterceptor(val context: Context) : Interceptor, AnkoLogger {
override fun intercept(chain: Interceptor.Chain): Response {
val builder = chain.request().newBuilder()
if (UserManager.jwtToken.isNotEmpty()) {
builder.header("Authorization", String.format("%s %s", "Bearer", UserManager.jwtToken))
}
val response = chain.proceed(builder.build())
if (response.code() == 401 && UserManager.islogged) {
UserManager.logout()
executeTransaction {
debug { "deleting all database" }
it.deleteAll()
}
val intent = Intent(context, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
context.runOnUiThread { context.toast(context.getString(R.string.session_expired)) }
}
return response
}
}
和日志:
05-22 09:31:25.771 21960-22425/com.sanilea.itineraire D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.771 21960-21960/com.sanilea.itineraire W/HttpRequestDataManager: SSL handshake timed out
05-22 09:31:25.772 21960-21960/com.sanilea.itineraire W/System.err: io.reactivex.exceptions.UndeliverableException: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.774 21960-21960/com.sanilea.itineraire W/System.err: at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
at io.reactivex.internal.operators.single.SingleCreate$Emitter.onError(SingleCreate.java:82)
at com.sanilea.itineraire.managers.StreetTypesManager$getAndSave$$inlined$getAndSave$1$2.accept(HttpRequestDataManager.kt:52)
at com.sanilea.itineraire.managers.StreetTypesManager$getAndSave$$inlined$getAndSave$1$2.accept(HttpRequestDataManager.kt:21)
at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
05-22 09:31:25.775 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
05-22 09:31:25.776 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
05-22 09:31:25.777 21960-21960/com.sanilea.itineraire W/System.err: Caused by: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.778 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
05-22 09:31:25.779 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
05-22 09:31:25.780 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-22 09:31:25.781 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-22 09:31:25.782 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
05-22 09:31:25.783 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
05-22 09:31:25.786 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
05-22 09:31:25.787 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-22 09:31:25.790 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-22 09:31:25.791 21960-21960/com.sanilea.itineraire W/System.err:
at com.sanilea.itineraire.http.JwtInterceptor.intercept(JwtInterceptor.kt:36)
05-22 09:31:25.792 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
05-22 09:31:25.793 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:42)
at io.reactivex.Observable.subscribe(Observable.java:10981)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10981)
at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
at io.reactivex.Single.subscribe(Single.java:2801)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
05-22 09:31:25.794 21960-21960/com.sanilea.itineraire W/System.err:
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
05-22 09:31:25.798 21960-22426/com.sanilea.itineraire D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.802 21960-21965/com.sanilea.itineraire I/zygote: Do partial code cache collection, code=122KB, data=78KB
05-22 09:31:25.809 21960-21965/com.sanilea.itineraire I/zygote: After code cache collection, code=122KB, data=78KB
05-22 09:31:25.810 21960-21965/com.sanilea.itineraire I/zygote: Increasing code cache capacity to 512KB
答案 0 :(得分:0)
问题来自我的模拟器,它完全被打破了......