UnknownHostException in android : Wifi issue

2019-09-04 00:40发布

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...

2条回答
We Are One
2楼-- · 2019-09-04 01:06

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.
    
查看更多
Explosion°爆炸
3楼-- · 2019-09-04 01:23
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

查看更多
登录 后发表回答