Retrofit on Android 6.0 has a problem making Http calls after connecting to Access Point
Steps to reproduce:
- Connect to Esp8266 Access Point
- Make an http call to http://192.168.4.1 (Default gateway of esp8266 accesspoint) The IP address of WIFI is 192.168.4.2
- It throws the below exception
I have tried the same on Android 5.1 and the same code works flawlessly
java.net.SocketException: socket failed: ENONET (Machine is not on the network)
at libcore.io.IoBridge.socket(IoBridge.java:619)
at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
at java.net.Socket.checkOpenAndCreate(Socket.java:689)
at java.net.Socket.setSoTimeout(Socket.java:543)
at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:183) at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170) at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111) at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187) at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123) at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93) at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at okhttp3.RealCall.getResponse(RealCall.java:243)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163) at okhttp3.RealCall.access$100(RealCall.java:30)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: socket failed: ENONET (Machine is not on the network)
at libcore.io.Posix.socket(Native Method)
at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
at libcore.io.IoBridge.socket(IoBridge.java:604)
at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
at java.net.Socket.checkOpenAndCreate(Socket.java:689)
at java.net.Socket.setSoTimeout(Socket.java:543) at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:183) at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170) at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187) at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123) at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93) at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) at okhttp3.RealCall.getResponse(RealCall.java:243) at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.access$100(RealCall.java:30)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588 at java.lang.Thread.run(Thread.java:818)
ENONET (Machine is not on the network)
can have several reasons. The most probable one is that you try to do the socket connection too early, i.e. you don't wait for the android smartphone to be fully connected to the network. Please print out your own IP address before the HTTP request to ensure that. The other thing could be wrong DHCP settings on the ESP created network (like network mask or network gateway). Can you connect to the network with a normal computer? What IP does it draw? Can you ping the gateway? – Maximilian Gerhardt