开发者

Catching SocketTimeOut error and responding

I have an activity that will require jsoup to connect to a url.

The problem is every now and again i will get

09-21 23:11:56.140: WARN/System.err(5725): java.net.SocketTimeoutException: Connection timed out
09-21 23:11:56.140: WARN/System.err(5725):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
9-21 23:11:56.140: WARN/System.err(5725):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:369)
09-21 23:11:56.140: WARN/System.err(5725):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
09-21 23:11:56.140: WARN/System.err(5725):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
09-21 23:11:56.140: WARN/System.err(5725):     at java.net.Socket.connect(Socket.java:901)
09-21 23:11:56.140: WARN/System.err(5725):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
09-21 23:11:56.140: WARN/System.err(5725):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)

I want to figure out a way when this happens to keep trying until after lets say 3 tries.

i was thinking catching the exception and then having for loop counter. I could use some implementation of this with some ans开发者_开发技巧wers.

Any suggestions?


To confirm, you wish to perform a specific action that has the possibility of transient failure, returning an object when that action is successful?

I've used the following pattern previously for this.

public class RetryUtil {
    public static <T> tryAction(Callable<T> action, int maxTimes);
}

Implement it such that it returns the result of the Callable when that function succeeds and tries again (using a loop as you suggest) when it fails.

You then define the repeatable action in an anonymous Callable and pass that in to tryAction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜