My program is downloading data from a server, once he received all the data, my program handles the data (performs checks like if that data is still on the server) and saves it into my database.
Now I was wondering what if my internet connection suddenly is disconnected, what happens with the data that I just was receiving. So I did a test. When I break the internet connection, my program stops fetching the data and goes to my method to check the data.
Now this wrong, because he will set some data already in my database that it isn't any more on the server, when it actually is.
The server is accessed by an API (webservices).
So my question is what is a good way to handle this kind of si开发者_JAVA技巧tuation?
Have you tried catching a WebException and handling it appropriately?
You need to find some way to determine if all of the data has been transferred. Either an end-of-file type of sequence in the data or you have a command that tells you how much data there is to be downloaded before you start downloading the data. That way, you can check if the connection is broken (ie. you stop receiving data) if you received everything you were supposed to.
精彩评论