bugfix> android > 投稿

開発したAndroidプロジェクトが1つありますKotlin + MVPアーキテクチャ。プロジェクトには合計30のAPI呼び出しがありますが、1つのAPIからランダムにソケットタイムアウトエラーがスローされる場合があります。サーバーまたはAPIまたはKotlin-MVPまたはRx2AndroidNetworkingに問題があるのか​​わかりませんか?

以下では、Kotlin + MVPで製品リストWeb API呼び出しを呼び出すことを示しています。

私が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;
        }
    })
}

performProductListApiCall関数(この関数は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);
                }

回答 1 件
  • before :

    .setOkHttpClient(okHttpClient)

    add those lines :

    OkHttpClient okHttpClient= new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.MINUTES); // connect timeout
    client.setReadTimeout(15, TimeUnit.MINUTES);    // socket timeout
    
    

あなたの答え