Android (Java) HttpURLConnection silent retry on 'read' timeout

前端 未结 4 890
春和景丽
春和景丽 2020-12-23 23:39

So I\'m using Google Volley for HTTP request, which basically uses Java\'s HttpURLConnection.

According to my tests, the

4条回答
  •  心在旅途
    2020-12-24 00:18

    Well, lot of time passed so I figured I will answer with my current (not perfect) solution for other people to easily see it (also written inside the question).

    Just set the readTimeout to 0 (no timeout) or at least the connection timeout value. This will make sure connection timeout will be raised before the read timeout.

    int timeoutMs = request.getTimeoutMs();    //  or whatever
    connection.setConnectTimeout(timeoutMs);   //  actual timeout desired
    connection.setReadTimeout(timeoutMs);      //  >= timeoutMs or 0
    

    Of course, it depends on the use of the request. Most of the times, the app is part of a project containing the related server, and you know read timeouts shouldn't happen anyway..

    As said, not really a "solution", but a very nice and 90% useful fix.

提交回复
热议问题