简体   繁体   English

我该怎么办才能解决连接问题?

[英]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.

相关问题 我可以使用哪种数据结构或设计模式来解决此问题 - What data structure or design pattern can I use to resolve this issue 如何解决异常:丢弃连接 - How can I resolve the exception: discard connection 如何解决超时问题? - How do i resolve the timeout issue? 如何在 SonarCloud 上解决此问题? - How can I resolve this issue on SonarCloud? NumberFormatException 的含义是什么以及如何解决它? - What is the meaning of NumberFormatException and how I can resolve it? 本地主机上的服务器 apache-tomcat-9.0.55 无法启动——不知道我能做些什么来解决这个问题 - Server apache-tomcat-9.0.55 at localhost failed to start -- no idea what I can do to resolve this 我如何解决硒中Mozilla Firefox 54上的不安全连接 - How can i resolve insecure connection on mozilla firefox 54 in selenium 我应该使用哪些设置来解决此GridBagLayout调整大小问题? - What settings should I use to resolve this GridBagLayout resizing issue? ArrayIndexOutOfBoundsException:3在glassfish中发出。 我该如何解决? - ArrayIndexOutOfBoundsException: 3 issue in glassfish. How do i resolve? 开始:小程序未初始化错误。 我该如何解决这个问题? - Start: applet not initialized error. How do I resolve this issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM