Android:SocketTimeoutException: failed to connect to /103.24.4.60 (port 80) after 30000ms

There are two possibilities:

1) Have you checked and tested your connection.

2) Better don't set any connection timeout,if you are setting chose maximum time, because it throws an error,if server didn't response within given time.

so you can use:

HttpUrlConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(7000);

or you can use:

for (int retries = 0; retries < 3; retries++) {
    try {
        final HttpClient client = createHttpClientWithDefaultSocketFactory(null, null);
        final HttpResponse response = client.execute(get);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new IllegalStateException("GET Request on '" + get.getURI().toString() + "' resulted in " + statusCode);
        } else {                
            return response.getEntity();
        }
    } catch (final java.net.SocketTimeoutException e) {
    // connection timed out...let's try again                
   }
}

hope so it will helps you, enjoy your code :)


I also had this problem. In my case, I was trying to connect my Android app with node REST api which was running on port 3000. I also had this connection timeout problem. My OS is windows.

Folow these steps.

1)- Check whether the mobile and Computer are connected to same network

2)-Check the computer IP assigned by router using below comand on cmd

ipconfig

The result will be like this

Windows IP Configuration

  Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : ********
   IPv4 Address. . . . . . . . . . . : ********
   Subnet Mask . . . . . . . . . . . : ********
   Default Gateway . . . . . . . . . : ********
                                   ***.***.8.1

Then check the used IP address. It must be IPv4 Address of the connected wifi network

3)-Set the network as private/ Home network

On windoes 10, you can do this by select taskbar wifi icon ==> selecte the properties of connected network ==> Under Network profile set the network as private

4)-Check the Firewall is disabled.

If your firewall is enabled please turn off it until test finish and remember to turn on it again after finishing your task


This issue can occur due to FIREWALL, please check below steps to resolve it

  1. Check whether you can connect directly through URL, using any of browser instead of Android app.
  2. If it fails, then it could be an issue with network connectivity not with android code.
  3. One of reason behind Network connection failure is "FIREWALL" option, which might be blocking connection request.
  4. In case of MAC machine, open "System Preferences==>Security & Privacy ==> Firewall ==> Turn Off Firewall". It will turn of FIREWALL on system.
  5. In case of other OS, please turn off FIREWALL with respective settings.

Now you should be able to connect through code as well direct through browser, hence there won't be any "SocketTimeoutException" error. Good Luck.

Tags:

Android