I’m writing an app using Flutter/Dart that uses websockets (package:web_socket_channel) and http (package:http) in different parts. I’m getting this error but only on Android (works on web), using websockets:
SocketException: Failed host lookup: ‘my_domain.fun’ (OS Error: No address associated with hostname, errno = 7)
The http connections in the same app (same physical device etc.) find the server no problem. The same code works fine when built for web and runs in the mobile browser. But when I build for android, websockets can’t seem to lookup my host. Any ideas?
The exception is being thrown in the file socket_patch.dart by _NativeSocket.lookup() method. Above that in the call stack there’s an async gap so I can’t see exactly where the code was before that.
I’ve tried building with different configurations and to preform a http connection first, hoping the hostname would be cached. Http worked, same failed lookup for websockets.
I already added:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
to my android manifest.
And confirmed the device has a good network connection. It works in the browser, just not the app.
The code is simple::
class MyClass{
// this is a class property
final WebSocketChannel channel = WebSocketChannel.connect(Uri.parse('wss://my_domain.fun'));
// constructor connects to listen to the webstream
MyClass() {
channel.stream.listen((message) {
print(message);
// more things happen
});
}
But it doesn’t find the host.
I’m now discovering that despite this exception the websocket connection does work
2
Answers
It seems this exception does not prevent the socket from working. Solution was to ignore :/
For the record: this is on a device not an emulator and I followed the many, many, many suggestions from other questions to add permissions to my manifest.xml before posting, which does not stop the exception (which does not stop the socket from working, weird).
This error is basically shown because of internet issue.
Sometimes some plugins require internet permission. Go to your
androidappsrcmainAndroidManifest.xml
path and set the permission undermanifest
tag and beforeapplication
tag.