Request in Android always giving socket timeout exception

梦想与她 提交于 2019-12-11 06:35:02

问题


I'm trying to make two requests when user clicks on a button. However, the request might take a little while.

When the user is on battery save mode and screens lock his device while the request is still being done, the request will not complete and will give a socket timeout exception.

I made a sample project to try this out and you can find it here.

I'm using retrofit and RxJava to make my requests like this:

 networkFactory.request()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        { result -> Log.d("TAG", "The value is this $result") },
                        { error -> Log.e("TAG", "Ohoh an error ${Log.getStackTraceString(error)}")
                        })

My networkFactory request() is:

 fun request(): Observable<Doc> {
        return service.request(API_KEY)
 }

with the following interface:

@GET("articlesearch.json")
fun request(@Query("api-key") apiKey : String) : Observable<Doc>

Am I doing something wrong here?

来源:https://stackoverflow.com/questions/45218070/request-in-android-always-giving-socket-timeout-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!