[英]What can I do to resolve the connection issue?
I have an app that uses JSoup to connect to a web server, and it works fine. 我有一个使用JSoup连接到Web服务器的应用程序,它运行良好。 Unfortunately, the said web server isn't very reliable. 不幸的是,上述网络服务器不是很可靠。 I get SocketException because of time-out connection quite often. 我得到SocketException因为超时连接的到处都是。 I make the connection in a modified IntentService, and I just simply repeat onHandleIntent(intent) in the catch(Exception e) block. 我在修改后的IntentService中建立连接,而只是在catch(Exception e)块中重复onHandleIntent(intent) 。
catch(Exception e){
Log.d(Tag, "in catch Exception block...");
onHandleIntent(intent);
}
Theoretically, this should work. 从理论上讲,这应该起作用。 But sometimes, I get stack over flow error, and the app ended quite ungracefully. 但是有时,我收到堆栈溢出错误,并且该应用程序非常不合常规地结束。 So, what can I do to make it better? 那么,我该怎么做才能使其变得更好?
I want to continue to call onHandleIntent, so, maybe I have to call it in iteration instead of recursively. 我想继续调用onHandleIntent,因此,也许我必须在迭代中而不是递归地调用它。 If you can give me advice on how to implement this iteratively, it would be very helpful. 如果您可以给我有关如何迭代实现的建议,那将非常有帮助。 Thanks! 谢谢!
I want to continue to call onHandleIntent, so, maybe I have to call it in iteration instead of recursively. 我想继续调用onHandleIntent,因此,也许我必须在迭代中而不是递归地调用它。
That is correct. 那是对的。 If you handle this recursively, a server that continually times out will inevitably result in a stack overflow. 如果递归地处理此问题,则服务器持续超时将不可避免地导致堆栈溢出。
If you can give me advice on how to implement this iteratively, it would be very helpful. 如果您可以给我有关如何迭代实现的建议,那将非常有帮助。 Thanks! 谢谢!
Something like this: 像这样:
for (int tries = 1; ; tries++) {
Connection conn = null;
try {
// attempt to connect
// do stuff
} catch (SocketException ex) {
if (/* timed out */ && tries < MAX_TRIES) {
continue;
}
// report exception
} finally {
if (conn != null) {
// close it
}
}
break;
}
(Maybe someone can think of a less "clunky" way to write this ...) (也许有人会想到一种不太“笨拙”的方式来写这个……)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.