skip to Main Content

My local API setup is running in localhost:3000on my Windows 10. I am using Retrofit to connect with APIs. I used http://10.0.2.2:3000 for connecting Android emulator to Window’s localhost. It was working fine for several months. One day it stopped working and I tried so many Google/Stackoverflow posts, but nothing helps. I tried in another newly installed machine and was not working. One day surprisingly it started working without any changes. Now I got my new PC and it is not working. Seems some instability with the 10.0.2.2 trick. Any ideas?

I am getting the below exception

java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 3000) from /10.0.2.16 (port 51498) after 60000ms

2

Answers


  1. Well, as you are asking for ideas, I am not sure if it’s work or not, but if I was working on this issue, probably my first step would be to update the retrofit for the last version, it seems the last version is Version 2.9.0 (2020-05-20).
    If it’s not working or I can update it, I would fake a DNS in Windows.

    To do that, I would include a new IP address in c:WindowsSystem32Driversetchosts:

    10.0.2.2 myserver-k.com www.myserver-k.com
    

    I hope it helps, but if not, give more context for non-android developers to try to help you, as I’m trying now.

    Login or Signup to reply.
  2. So, it seems the problem is not in the server. So let’s check what could be in the android app.

    Did you check:

    1. Network Permissions: on AndroidManifest.xml file:
    <uses-permission android:name="android.permission.INTERNET" />
    
    1. Cleartext Traffic: Connecting to an http:// URL, and your app’s target SDK is 28 (Android 9.0) or above, Android’s default network security policy will block your connection. You can enable clear text traffic for your domain in your app’s network_security_config.xml:
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">yourserver.com</domain>
        </domain-config>
    </network-security-config>
    

    and

    <application
        android:networkSecurityConfig="@xml/network_security_config"
        ... >
        ...
    </application>
    

    If it’s not work, could you set the retrofit to log the network request ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search