-
-
Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.
// Customize OkHttp to add IPv4 DNS preference | |
import com.facebook.react.modules.network.OkHttpClientProvider; | |
import com.facebook.react.modules.network.OkHttpClientFactory; | |
import okhttp3.OkHttpClient; | |
public class CorePlaneOkHttpClientFactory implements OkHttpClientFactory { | |
public OkHttpClient createNewNetworkModuleClient() { | |
return OkHttpClientProvider.createClientBuilder() | |
.dns(new CorePlaneOkHttpDNSSelector(CorePlaneOkHttpDNSSelector.IPvMode.IPV4_FIRST)) | |
.build(); | |
} | |
} |
import okhttp3.Dns | |
import java.net.Inet4Address | |
import java.net.Inet6Address | |
import java.net.InetAddress | |
import java.util.logging.Logger | |
class CorePlaneOkHttpDNSSelector(private val mode: IPvMode) : Dns { | |
enum class IPvMode(val code: String) { | |
SYSTEM("system"), | |
IPV6_FIRST("ipv6"), | |
IPV4_FIRST("ipv4"), | |
IPV6_ONLY("ipv6only"), | |
IPV4_ONLY("ipv4only"); | |
companion object { | |
@JvmStatic | |
fun fromString(ipMode: String): IPvMode = | |
IPvMode.values().find { it.code == ipMode } ?: throw Exception("Unknown value $ipMode") | |
} | |
} | |
override fun lookup(hostname: String): List<InetAddress> { | |
var addresses = Dns.SYSTEM.lookup(hostname) | |
addresses = when (mode) { | |
IPvMode.IPV6_FIRST -> addresses.sortedBy { Inet4Address::class.java.isInstance(it) } | |
IPvMode.IPV4_FIRST -> addresses.sortedBy { Inet6Address::class.java.isInstance(it) } | |
IPvMode.IPV6_ONLY -> addresses.filter({ Inet6Address::class.java.isInstance(it) }) | |
IPvMode.IPV4_ONLY -> addresses.filter({ Inet4Address::class.java.isInstance(it) }) | |
IPvMode.SYSTEM -> addresses | |
} | |
//logger.fine("DJMOKHTTP ($hostname): " + addresses.joinToString(", ") { it.toString() }) | |
return addresses | |
} | |
companion object { | |
private val logger = Logger.getLogger(CorePlaneOkHttpDNSSelector::class.java.name) | |
} | |
} |
public class MainApplication extends Application implements ReactApplication { | |
// ... | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); | |
} | |
// ... | |
} |
Hi @andreialecu : Related to issue aws-amplify/amplify-js#5539
I tried to apply this solution, but it don't work now
Do you have any other solution to fix this ?
Thank you so much
This is the solution I'm using and it works for me.
This fixed my issue too. Now I'm able to make network calls right after app opens, no need to wait timeout or fallback to ipv4 for 3 minutes.
Helped me as well! Thank you
Hi, this solution works on debug but not on the release, any workaround?
when i try to compile with this i get:
cannot find symbol
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory());
^
symbol: variable OkHttpClientProvider
location: class MainApplication
what is the solution?
How can I apply this to an Expo app?
@andreialecu @andac-ozcan @danmaas
when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?
@HodayaGruz I also got this error, did You find how to solve it?
when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?
@HodayaGruz I also got this error, did You find how to solve it?
You should import this:
import com.facebook.react.modules.network.OkHttpClientProvider;
Thanks for this!
For anyone else landing here that isn't a Java developer, for this to work I also needed to add
package my.package.name;
to the top of both the.java
and.kt
files. (same package name as you have at the top ofMainApplication.java
And also needed
apply plugin: 'kotlin-android'
inandroid/app/build.gradle
.Here's a related thread with further details:
facebook/react-native#32730