Last active
April 10, 2023 04:27
-
-
Save Doko-Demo-Doa/21a55ad72648fa54f4a3e7ecfa8a5017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.yourapp; | |
import com.facebook.react.modules.network.OkHttpClientProvider; // Import this. | |
... | |
public class MainApplication extends Application implements ReactApplication { | |
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { | |
... | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
SoLoader.init(this, /* native exopackage */ false); | |
OkHttpClientProvider.setOkHttpClientFactory(new CustomNetworkModule()); // Add this line | |
} | |
} |
Author
Doko-Demo-Doa
commented
Apr 10, 2023
•
package vn.edu.dora.app.modules.customnetwork
import com.facebook.react.modules.network.OkHttpClientFactory
import com.facebook.react.modules.network.ReactCookieJarContainer
import okhttp3.OkHttpClient
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.*
class CustomNetworkModule : OkHttpClientFactory {
override fun createNewNetworkModuleClient(): OkHttpClient {
val trustAllCerts = arrayOf<TrustManager>(
object : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> {
return emptyArray()
}
override fun checkClientTrusted(
certs: Array<X509Certificate>, authType: String
) {
}
override fun checkServerTrusted(
certs: Array<X509Certificate>, authType: String
) {
}
}
)
try {
val sc = SSLContext.getInstance("SSL")
sc.init(null, trustAllCerts, SecureRandom())
val sslSocketFactory: SSLSocketFactory = sc.socketFactory
return OkHttpClient.Builder()
.dns(CustomDNS())
.connectTimeout(10, TimeUnit.SECONDS)
.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
.hostnameVerifier(HostnameVerifier { hostname, session ->
true
})
.cookieJar(ReactCookieJarContainer())
.build()
} catch (e: Exception) {
return OkHttpClient.Builder()
.dns(CustomDNS())
.cookieJar(ReactCookieJarContainer())
.build()
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment