У меня есть один проект Android, который я разработал в архитектуре Kotlin + MVP .Он имеет в общей сложности 30 вызовов API в проекте, но иногда он случайным образом выдает ошибку времени ожидания сокета из любого одного API.Я не знаю, это проблема в сервере или API, Kotlin-MVP или Rx2AndroidNetworking?
Ниже я покажу вам, как вызвать список вызовов через веб-интерфейс API в Kotlin + MVP.
Это тот же проект, который я разработал в iOS, и эта ошибка не выдается, поэтому она не является ошибкой при разработке сервера или API.Я думаю, что эта проблема возникла в Rx2AndroidNetworking
библиотеке.
Кто-нибудь имел какие-либо идеи или сталкивался с этой проблемой в прошлом?
java.net.SocketTimeoutException
failed to connect to / (port 80) after 120000ms: isConnected failed: ETIMEDOUT (Connection timed out)
at libcore.io.IoBridge.isConnected(IoBridge.java:232)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:70)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:158)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
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)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
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)
at okhttp3.RealCall.execute(RealCall.java:77)
at com.rx2androidnetworking.Rx2InternalNetworking$SimpleANObservable.subscribeActual(Rx2InternalNetworking.java:187)
at io.reactivex.Observable.subscribe(Observable.java:10981)
at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
at io.reactivex.Observable.subscribe(Observable.java:10981)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
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:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
ProductInteractor.kt
class ProductInteractor @Inject internal constructor(apiHelper: ApiHelper, preferenceHelper: PreferenceHelper) : BaseInteractor(apiHelper = apiHelper, preferenceHelper = preferenceHelper), ProductMVPInteractor {
override fun getProductList(pagingAndSortingInfo: PagingAndSortingInfo?, searchBy: String, categoryIds: String) =
apiHelper.performProductListApiCall(pagingAndSortingInfo, searchBy, categoryIds)
}
ProductPresenter.kt (it.showError(t!!.message!!)
Тайм-аут выдачи сокета для строки выдачи)
class ProductPresenter<V : ProductGridMVPView, I : ProductMVPInteractor> @Inject internal constructor(interator: I, schedulerProvider: SchedulerProvider, compositeDisposable: CompositeDisposable) : BasePresenter<V, I>(interactor = interator, schedulerProvider = schedulerProvider, compositeDisposable = compositeDisposable), ProductMVPPresenter<V, I> {
override fun onViewPrepared(pageNumber: Long?, categoryIds: String?) {
getView()?.showProgress()
interactor?.let {
val pagingAndSortingInfo = PagingAndSortingInfo(AppConstants.PAGE_SIZE.toLong(),
pageNumber,
0,
true,
true,
"DisplayOrder",
AppConstants.SortDirection.Ascending.type)
var disposable = it.getProductList(pagingAndSortingInfo, "", categoryIds.toString())
.compose(schedulerProvider.ioToMainObservableScheduler())
.subscribe ({ productResponse ->
getView()?.let {
it.hideProgress();
var totalRecords: Long? = 0;
var currentPage: Long? = 0;
var isPagingRequired: Boolean? = false;
totalRecords = productResponse.pagingAndSortingInfo!!.totalRecord;
currentPage = productResponse.pagingAndSortingInfo!!.pageNumber;
isPagingRequired = productResponse.pagingAndSortingInfo!!.isPagingRequired;
it.displayProductList(productResponse.productItems, totalRecords, currentPage, isPagingRequired);
}
},{
t: Throwable? -> getView()?.let {
it.hideProgress()
it.showError(t!!.message!!)
}
})
addSubscription(disposable)
}
}
}
ProductGridFragment.kt (я вызываю класс докладчика, как показано нижев моем фрагменте продукта onViewCreated.)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
presenter.onAttach(this)
super.onViewCreated(view, savedInstanceState)
srl_product.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener {
override fun onRefresh() {
productAdapter.clearData()
setPageNumber(1);
setCategoryIds("");
presenter.onViewPrepared(getPageNumber(), getCategoryIds());
srl_product.isRefreshing = false;
}
})
}
функция executeProductListApiCall (эта функция вызывается в классе Interactor)
override fun performProductListApiCall(request: PagingAndSortingInfo?, searchBy: String?, categoryIds: String?): Observable<ProductResponse> =
Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_PRODUCT_GETLIST)
.addHeaders("Authorization", "bearer " + prefHelper.getAccessToken())
.addQueryParameter("searchBy", searchBy)
.addQueryParameter("categoryIds", categoryIds)
.addBodyParameter(request)
.setOkHttpClient(okHttpClient)
.build()
.getObjectObservable(ProductResponse::class.java)
.doOnError {
t: Throwable -> Log.e("performProductListApiCall", t.message);
}