UnknownHostException in android : Wifi issue

一笑奈何 提交于 2020-01-07 05:11:20

问题


I have get UnknownhostException in parsing JSON data from server,

My URL working on :

http://jsonlint.com

on real device browser(data plan only)

Sometimes its working on PC browser and sometimes not.

Actually I have get the wifi issue, Sometimes its working on wifi connection, when its not, I have restarted my wifi and its worked, and after some time the same issue came,

I want to get the permanent solution for this wifi connection issue, Restarting the Wifi is never a solution on the application user side,

What is the exact problem and solution also...


回答1:


Usually the UnknownHostException fires when you cannot resolve the DNS record of the URL you've provided. There's a reasonable timeout for that operation, but if you have a weak WiFi connection or you don't have enough signal on your device, the communication can be interrupted in the middle between sending and receiving the response, so your device doesn't receive the response, thus it thinks it's a DNS timeout.

There are 2 things you can try:

  • Increase the timeout of the response. This will not help, though, if your communication gets interrupted you already sent the query.

  • Use the IP address instead:

    shut-up@i-kill-you:~$ ping jsonlint.com
    PING jsonlint.com (54.243.171.164) 56(84) bytes of data.
    



回答2:


WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                        int linkSpeed = wifiManager.getConnectionInfo().getRssi();

                            System.out.println("Link Speed is======"+linkSpeed);

It will give u wifi speed so just mention one speed if that speed come then only do next work



来源:https://stackoverflow.com/questions/21726387/unknownhostexception-in-android-wifi-issue

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