-
-
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, 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;
Helped me as well! Thank you