简体   繁体   中英

How to shutdown an android app if it takes too long to load? (slow/no internet)

I'm making an android app that gets internet data from a website. It uses JSON parsing, loadurl, LoadImageFromWeb, HttpClient and other stuffs. My problem is when the internet connection is slow, it takes a long time to load (if no internet it will load forever). It will load eventually but then the data are not there, just the layout and buttons and other stuffs that don't need internet. I want to know how to shutdown the application and bring up a dialog if its too long to load (or in the other case, it loaded but there's no data). I don't know how and where to put it since I have many different functions. Thanks in advance :)

Ok, first, this code shows how to check if you have no Internet connectivity. If you do this in your default activity, then if it shows no connectivity, pup up a Toast message and shutdown for example.

Check internet connectivity

Second, you may want to have the code for your http client set timeouts and if they return then set a toast message and close. If the calls are all over you probably need to rearchitect a bit to tie them together better? Can't really tell without seeing your code.

See this code for setting socket timeouts on the http client:

HTTP timeouts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM