问题
I have written an app that hits a website for data every hour or so. In most cases, this works just great. However, I've had repeated issues lately where suddenly the app stops updating the data. In other words, the app is working just great, accessing the website and pulling down data...and then suddenly it won't anymore. I finally tracked it down to a "java.net.UnknownHostException: www.mysite.com".
The thing is...the device (this is on devices, NOT the emulator) can still access the internet, so it is not a network access issue. The other thing is, this never clears up on its own...it will keep giving this error for DAYS. So far the only thing that I've found fixes it is a phone reboot.
Unfortunately, this hasn't ever happened on my phone, so I can't test it on my own. I'm just hearing about it from other people.
Is there some way to "reboot" the part of the phone network interface that resolves domain names, if that is the issue?
Here is the code I use to access the website:
try {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
if (entity == null) {
throw new Exception("Entity was null");
}
line = EntityUtils.toString(entity);
}
catch (ClientProtocolException cpe) {
throw new NetworkNotAvailableException("A client protocol exception occurred: " + cpe.getLocalizedMessage());
}
...and more catches below this
来源:https://stackoverflow.com/questions/8957557/unknownhostexception-suddenly-starts-happening-and-doesnt-go-away-on-phone-n