UnknownHostException suddenly starts happening, and doesn't go away (on phone, not in emulator)

僤鯓⒐⒋嵵緔 提交于 2019-12-10 17:58:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!