Skip to content Skip to sidebar Skip to footer

React Native Https Api Calls Works In Ios But Not Working In Android

So basically what I'm doing is simply making simple Axios call to our UAT server which has HTTPS enabled. I've tested the entire application in IOS API calls are working fine there

Solution 1:

Step1 : create a java file ex: 'CustomClientFactory' inside directory app > Java > com > (your_app_name) > CustomClientFactory.java

Inside that java file add the following code:

import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;

import java.security.cert.CertificateException;
import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.TimeUnit;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

importstatic android.content.ContentValues.TAG;

publicclassCustomClientFactoryimplementsOkHttpClientFactory {
privatestaticfinalStringTAG="OkHttpClientFactory";
@Overridepublic OkHttpClient createNewNetworkModuleClient() {
    try {
        // Create a trust manager that does not validate certificate chainsfinal TrustManager[] trustAllCerts = newTrustManager[]{
                newX509TrustManager() {
                    @OverridepublicvoidcheckClientTrusted(java.security.cert.X509Certificate[] chain, String authType)throws 
CertificateException {
                    }

                    @OverridepublicvoidcheckServerTrusted(java.security.cert.X509Certificate[] chain, String authType)throws 
CertificateException {
                    }

                    @Overridepublic java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        returnnewjava.security.cert.X509Certificate[]{};
                    }
                }
        };

        // Install the all-trusting trust managerfinalSSLContextsslContext= SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, newjava.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting managerfinalSSLSocketFactorysslSocketFactory= sslContext.getSocketFactory();



        OkHttpClient.Builderbuilder=newOkHttpClient.Builder()
                .connectTimeout(0, TimeUnit.MILLISECONDS).readTimeout(0, TimeUnit.MILLISECONDS)
                .writeTimeout(0, TimeUnit.MILLISECONDS).cookieJar(newReactCookieJarContainer());
        builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
        builder.hostnameVerifier(newHostnameVerifier() {
            @Overridepublicbooleanverify(String hostname, SSLSession session) {
                returntrue;
            }
        });

        OkHttpClientokHttpClient= builder.build();
        return okHttpClient;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        thrownewRuntimeException(e);
    }
}

}

and inside your MainApplication.java add the following:

@OverridepublicvoidonCreate() {
 super.onCreate();
 SoLoader.init(this, /* native exopackage */false);  
 OkHttpClientProvider.setOkHttpClientFactory(newCustomClientFactory()); //add this 
 line.
 } 

Found the above solution from : Similar issue

And it worked fine for me. As per I understand, we write this file to Install the all-trusting trust managers and asking the app to allow 'https' api

Solution 2:

I think it's because you don't have the internet permission in your AndroidManifest.xml. this link should help

Post a Comment for "React Native Https Api Calls Works In Ios But Not Working In Android"